From 21880ee880d321e760b6b560db0696c8af4c98d9 Mon Sep 17 00:00:00 2001 From: alyx Date: Sun, 3 Dec 2023 02:43:18 -0500 Subject: 2023.3 --- 2023/rs/code/three/Cargo.toml | 8 ++ 2023/rs/code/three/src/input.txt | 140 ++++++++++++++++++++++ 2023/rs/code/three/src/input2.txt | 239 ++++++++++++++++++++++++++++++++++++++ 2023/rs/code/three/src/main.rs | 96 +++++++++++++++ 4 files changed, 483 insertions(+) create mode 100644 2023/rs/code/three/Cargo.toml create mode 100644 2023/rs/code/three/src/input.txt create mode 100644 2023/rs/code/three/src/input2.txt create mode 100644 2023/rs/code/three/src/main.rs (limited to '2023/rs/code') diff --git a/2023/rs/code/three/Cargo.toml b/2023/rs/code/three/Cargo.toml new file mode 100644 index 0000000..6c6d234 --- /dev/null +++ b/2023/rs/code/three/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "three" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2023/rs/code/three/src/input.txt b/2023/rs/code/three/src/input.txt new file mode 100644 index 0000000..1125e9b --- /dev/null +++ b/2023/rs/code/three/src/input.txt @@ -0,0 +1,140 @@ +............677..........................................................................227.....730..35.......318...........92...166....... +....%..863..#......................36.............956..337%......692..............*744....$..........*......../.....187..-.................. +..346...*.....475.440....903&..996*...404+.395...*..............*.......&253.223.....................453..535......@....265.....290$........ +.........470.*.....+..........................*.........148...40....361...................883.............*....%............649............. +.............587..........#..........848*.............../.........................................415....445....947............*...509...... +......170..............922..79*660.......87...802...........................669.152...867...........*.........................926....+.53... +.......-..#319..915................846......../......120...870*874..610.........$....-.......577.....825.....430$.....689................... +......................700.........*................................*..................../.........................693.*....734&......@.59... +..........247..........*.......444....768....569.248..430..565.....82............525...794...171...................*..160........39.25.*.... +..........*.........560............56*......$..........&...*............70-........&.........*....397....571.....297.............*.....422.. +......338..481........../..#220...............116.........194..................#.....=....-..529..=........................../....381....... +.892....*....../...708.856.......405.............*324................&...922....60..288..810.........747............*876./....180........... +....*..41......456........................652..................48..110..+.....................199.....*..........824......471........261.... +..317......+1........29...316&...368&............%.365...212....*..............................*..292..70..............................*.... +.......................................*......616..-.....*....596........*699.978*259....309..660....*...........90....634.....631......987. +140........2*....285...836&..........13.8.............264.............945...............*............323....514.&.........-......%.......... +......&......634..*..........816...........................41..=..............@670......648................+.............................926 +......343.........221....157....*.......=..492.319..............659......238.........................-.......113.......187...863............ +........................*......937....471........*...274..................*....../244......22..332...908.......*..........#..*.............. +.......610.............670.693..................182...*..618.............865................................678........85.....948........... +.........*...334.............../.....21*853.........491..../...524...............494........794.....401.........$........*327............... +........270.../................164.......................#....=..............235...*....-..*.......*.............142..............738*...... +...579.............@...823...............596........+263.797.....916..............632..618..335.996......504*..................+......853... +....&...646*.872...805....*...-788.......*.........................*.......985*..............................440.......310..826.....+....... +..............*.........54..........104...238.........787...........79.........449.674........204......................*...........808...... +...949....396..................664....$.........626..*...............................*.......*....923..30....482........111.............+... +...$......*...................../.............*......387.........333...-........@.750.......737.........*.....*.............890..707.....852 +.........264....247.....416+..................177...........55..........251..911.........................88...741.................@......... +................*..............-........................232..*.....737...............597..............................=.+...............24.. +.................456............109..907................../...656.......................*..................855.451.958..783.........854*.... +.....65*....#188.......894............................@.....&................380.302.133..507.................*..............227............ +..71....14..................774..%109..821...182*......85...256......314.......%..........-....148..$.824*591......653........*......&435... +.........................$./.............@...........................=............228..............32.........*........%989..121............ +.......................64.......545.........*458......735....435...........869.......+...........=.........307.......................*...... +74...209../885..................*....&368.86.........*..........*....965.....................919..226................990..........381.566... +........#..........970.150....910.=..............973.502....$.603..#..*....429..557...#................665.......307........................ +...-.........486..........=.......514.............*......621......59.654......*...*....756...584.....%....*453...*....250...328...995....... +....997.........*.......................316......533........................100..485.........*.......955........792..........*.......*...... +.........420....111..........273.430.......*889..........824..338..91.55.............656....70...............................936...713...... +......#....*........647......*...../.484.......................*..*......#376..........................425.613.............................. +...568....151....$.......95.............*...66.....818..453.735..817.319......568*764...........242.....+.............829.1................. +..............216....345......................*.....*.................*...549............251.......*.........*44.......*...*352......597*7.. +.....138..240......$...*.............34..197.988.....760..981....*....682....*...263/...*.........19......678.......$..839.......70......... +........*...*....270.........147.700.......*...............+..470.849.......625.........218..$.....................936..........*....@...... +.......953..856.................*.........910........504.........................&..........830........874..................514.772..698.... +814...................460...........................$............&49......804..394......................*...............747...*............. +........876...........@...............607......15........8...................$.........706..............767...972.......*...912....%........ +.............805...83..............*......156..............924...719...............514*...........................936...........960..364.... +...692........*....#......220...667.645....*........................=...977..628........415...................@....*..386....#.........*.... +.............................$.............891.948..479...................-...*..........*...*385......*...173....113./...765..779......643. +..........$754......8............599...........*............373.............814....537.307..........552.........................*........... +......................*111..874..&...887....820.........73..*..........196.....................260.......%......915.774.........../......... +..........670*484.624..........$.......................%.....17........*.......91@..703*705...............298......*............160......... +.....256...............&..................833.....@......319....&310.778........................214...................195..............7.... +......*......-.......229.................*........23.........82.............604..428.....258&..*.................303..*......198............ +....554...262...621*...........959....475..28............836.*...11*68...........#............602..73.....+471..*....161.61.*...........652. +...............................*..........................$..324.........&.........-.....950........*..........921.......*...551...799$.$... +..............=306..19/.397*....127.......*..........794..........461....997........429.#.........923....................................... +..950.....225...............667........743.560.........*....678....*..................................240.............*..................... +...........*......................&727..................229...*...20............508@...464........312.....399..945...430.................... +............239......*453.....................................908.........339.........*........................*............394@............ +.606..647......................544.....*765.............908/...............*....318...393................481.445..................714....... +...*.../......859......362....../...352...........736.........970.......138....................%....850............529........#......*561... +..841.............*.......$................225...=....*470..................280....109......441........*785....281*........576..@929........ +...........146..34.965......272...............*....418......-....=918..862.*......*...................................#365.................. +.821*388......*.....................781......778...........839...........*..779.........635..#....243..+............*.............*493...147 +...............991....463...211*......$...........291...............+.............970...*....153..*....81..........614...720....63.......... +...........714..................127......81......&.................307.............-.....948.....565......907...........*................... +...........................-..............#...#...........................&..........145.....................*365..590..711....830....511... +..........136..-...810....9....................178...........*440....696...792..............260........18......................*............ +......364....*.193...+..........5*.....390+..........580..630...........*..............324./....463....*........................846......915 +.........*..............868.......593.......384......../....................201........*..............343..852......96...................... +......303..64.............................................493.........694............833....758........................266......531......... +.............*823..................................461............295..........100$.......%...=.884..433....929.......&.........$........... +521..........................&.................120.........................../..........996.....*.......&....*...971.................-...... +.......@..227...............440..308.../................152.......#.........82.660.%265......599.............646..*...505....547$.....983... +.....157................................22.......=.......%...&.....12....................................428.....641........................ +...........398.....381.../...248................492........631.....................128...../...577.......$............792................... +..........+...............28.........41.....386......15............*937......660...*....915.......*.594.................$.......964......... +..............216.............351&...*........*.....*........$..449...............328..........270...#.........900.........930.*.....941.... +..552.224.402*......895.............302.462....248...374...142................535..........................884....%..71....$...997...*...589 +.....*.....................270...-........=.-..........................760..........151..508..........21.....*......+....................... +........341..%898.......$......612...........103..............895....*.*.........@.....-.....99........*..939.....*......................... +........*............295.............591.................458.+.....374..70........284................283.........340..89......820......932.. +......284.......826........+838........*........*.........$...................................89........................&.....=.......*..... +..........818..........................741...175.495................489.........................*.........469..565..%...........613.32...... +788.982...*.........468..............................952....73...............588...336........+...498*805....*...*...332.........*.......... +....../.884...%.127....&..980...#.........214...............+.................*....*.......336...............812..43.......475..908......... +............294..............-.681......=......1........647.............180..895..760....%................91...............*.........674.... +...24..#.................739.............657..=....149.....*..296.737..*..............553...............*...*473........474...%............. +.......694.......*..%....*.........*..................-.117....*.....*..............................953.793....................653.....635.. +..............519....963..640......718......................*...693.83.............................$.................................-..#... +.......................................463...........*39..867...........200..................766.........893........262.89.777-.....476..... +......................@790........................983...................*.....................*.............*906...../..*.........%......... +.../.......417..619...................................................944...973.....#......664.........203...............504.824...626.149.. +...316...............847...........%....................382...1................*.825..223.............@.........+............*.........*.... +...............816....*.....744..771.628..............*....*..*..............433......*..........................303......569..725......221. +.....&543........*.774.......*.....................461..357..131...583..892$.........627...978..630...#653........................#......... +352............540............509.504*......109......................*.......................+.-...............431..486..250....%........... +...+.......774.....343........................*.......&............859.....499......................./................*......448....425..... +..............*857.$........26....+.....$..705..491.128......871............#..659....................119.....%80.770.25.................... +......66....................*..453....497.........-.......*.....*................@.....833..........................................-....... +.....*........196........535........................./.233.230.238.....................*...#.56*...........757.......620..........$..694.... +...226.122....*.....116...............78$.........652.....................+589......260..633....97....153...............*..$...142.......464 +........@......493..........883...862.................199......................100......................%.............445...25.....*........ +...........372.........388...*...%.......13*506......%...../855...-456.........+................373........@....976.............243.965..... +.....234..*........200...&.659...........................-...............353......451..........*..........997....*.......................... +........*..........*...........205..................#...346.................@....-.......890..455.....819.....673..733*473.................. +..........970...132...$....509*..........#.......437..........290..../489...........59.................*........................961......... +..972.192.@.........152.@.......$...876...993.............449....&.............335.*.....184.........723.......................+.......+.... +...............899......470.82..908..............10.......+...........375......#....635...*................*538.....$..228..%...........587. +...751........................-......................996....351........*..422...........246.......#393..168.......576...*...652...784....... +....................997.................49.............*...*....118......*.....998..$..........2............#....................*.......... +.........-749..612..*.............140..$.......659.....750..965....*...187.....*...973..............859..908......951.....104.....749...+... +.302...............257...........................*...............851...........444..........58*119...*........162....@......*............431 +....*........260*........324................262...147.......+..............245........................604.47...............333..../......... +.................926........*248...............*..........660................*......438=..250.................622................103....543. +............@223............................933.........................605..285............*..................*.....622...............#.... +....&...873.............6..530.......251.........17..529...................*.................945...905.........505...*.....181.............. +.....60....*........94..&...&..........#........+....../....746*768........849.......985*445.............*...........201..$....307...-...... +..........838.365..$.............595+..............-............................256..............533......84..................%.......564... +.....855........*.....%...............854.........20..739..........+............................................992......................... +.981..*..........75..147.390..................842....../..997......145.......593..............#...........769../........88..............323. +...*..911.................*..997.........................*................15*.......642....327........148*................*268..........@... +..127.........................@...........856........284.753..469.....+.......611=.....&.........................63....&........@346........ +......758..............670...............*.....431..............*.460..988..=...................................*.....538..............#697. +.........................*.......311.=37.........*............524...*.......315..316..902.....864*.....%529.....392.......859..156@......... +.....$..............610......478.#..............335................337.................-..........828........................#......530.191. +.892.914......................%..........................283....................208........626.........&.40.....443..626.$.............*.... +...*.....................963....645*........694*.................539.....317..................%......555............*.....450......+........ +..412..422..373%...713$..%......................778.159.........*...........#.......978...................+......647..........$..885....&... +...............................58...........383.....%..........185...376...........+.....158.........%.....398.............203.........784.. +......360...914*875.104&........&.673....-.....*.*....................*...................*.....590.417.60.......................*.......... +.......*.................................386.592..445...............74...........&........864.............$.370.....75........125.102...292. +........950.522..422&.204%......918.........................228................918............../..............-...*...951.................. +.....@.........*..................................................228..840$.....................584......72..#....563...$..717..113......... +..250...........817.........#........614...224...164...506......................362....873...........568*....920...............*............ +......159.................553..294#...*.........*......................42..$...........*......884.......................950...482..611...249 +........*....978*275.................974...*....81.........../...........%.405.........224.....-....*......409*20....../............*...*... +.122.273...............759....137...........574............229...................................497.41............................872.78... diff --git a/2023/rs/code/three/src/input2.txt b/2023/rs/code/three/src/input2.txt new file mode 100644 index 0000000..c870508 --- /dev/null +++ b/2023/rs/code/three/src/input2.txt @@ -0,0 +1,239 @@ + .... +.101. +..... + +. ... +.102. +..... + +.. .. +.103. +..... + +... . +.104. +..... + +.... +.105. +..... + +..... +.106 +..... + +..... +.107. +.... + +..... +.108. +... . + +..... +.109. +.. .. + +..... +.110. +. ... + +..... +.111. + .... + +..... + 112. +..... + + ... +201. +.... + +. .. +202. +.... + +.. . +203. +.... + +... +204. +.... + +.... +205 +.... + +.... +206. +... + +.... +207. +.. . + +.... +208. +. .. + +.... +209. + ... + + ... +.301 +.... + +. .. +.302 +.... + +.. . +.303 +.... + +... +.304 +.... + +.... +.305 +... + +.... +.306 +.. . + +.... +.307 +. .. + +.... +.308 + ... + +.... + 309 +.... + + ...... +....... +..001.. +....... +....... + +. ..... +....... +..002.. +....... +....... + +.. .... +....... +..003.. +....... +....... + +... ... +....... +..004.. +....... +....... + +.... .. +....... +..005.. +....... +....... + +..... . +....... +..006.. +....... +....... + +...... +....... +..007.. +....... +....... + +....... +...... +..008.. +....... +....... + +....... +....... +..009. +....... +....... + +....... +....... +..010.. +...... +....... + +....... +....... +..011.. +....... +...... + +....... +....... +..012.. +....... +..... . + +....... +....... +..013.. +....... +.... .. + +....... +....... +..014.. +....... +... ... + +....... +....... +..015.. +....... +.. .... + +....... +....... +..016.. +....... +. ..... + +....... +....... +..017.. +....... + ...... + +....... +....... +..018.. + ...... +....... + +....... +....... + .019.. +....... +....... + +....... + ...... +..020.. +....... +....... diff --git a/2023/rs/code/three/src/main.rs b/2023/rs/code/three/src/main.rs new file mode 100644 index 0000000..c72abe9 --- /dev/null +++ b/2023/rs/code/three/src/main.rs @@ -0,0 +1,96 @@ +#![feature(iter_map_windows)] + +use std::collections::BTreeSet; + +static INPUT: &[u8] = include_bytes!("input.txt"); + +#[derive(Copy, Clone, Debug)] +enum LineMaskEntry { + Empty, + Gear, + Number{tag: u32, num: u32} +} + +fn main() { + let valueified = INPUT.split(|b| *b == b'\n').filter(|s| !s.is_empty()); + + let mut num = 0; + + let (parts_number_sum, gears_mask) = [valueified.clone().next().unwrap()].into_iter().chain(valueified.clone()).chain([valueified.clone().last().unwrap()]) + .map_windows::<_, _, 3>(|[prev, curr, next]| { + let mut numlen = 0; + let mut numval = 0; + + let mut acc = 0; + let mut linemask = vec![LineMaskEntry::Empty; curr.len()]; + + for i in 0..curr.len() { + if curr[i] == b'*' { linemask[i] = LineMaskEntry::Gear; } + + if !curr[i].is_ascii_digit() && numlen != 0 { + let start = if i - numlen == 0 { i - numlen } else { i - numlen - 1 }; + let end = i; + let window = || start..=end; + if prev[window()].into_iter().chain(&curr[window()]).chain(&next[window()]).any(|b| !b.is_ascii_digit() && *b != b'.') { + acc += numval; + + linemask[i - numlen .. i].fill(LineMaskEntry::Number{ tag: num, num: numval }); + num += 1; + } + + numlen = 0; + numval = 0; + } + else if curr[i].is_ascii_digit() { + numlen += 1; + numval *= 10; + numval += (curr[i] - b'0') as u32; + } + } + + if numlen != 0 { + let window = || curr.len() - numlen - 1..curr.len(); + if prev[window()].into_iter().chain(&curr[window()]).chain(&next[window()]).any(|b| !b.is_ascii_digit() && *b != b'.') { + acc += numval; + + linemask[curr.len() - numlen .. curr.len()].fill(LineMaskEntry::Number{ tag: num, num: numval }); + num += 1; + } + } + (acc, linemask) + }).fold((0, Vec::new()), |(n, mut v), (a, l)| (n + a, {v.push(l); v})); + println!("Sum of valid part numbers: {parts_number_sum}"); + + + let first = vec![LineMaskEntry::Empty; gears_mask[0].len()]; + let last = first.clone(); + let gear_ratio_sum = [first].into_iter().chain(gears_mask).chain([last]) + .map_windows::<_, _, 3>(|[prev, curr, next]| { + let mut acc = 0; + + for i in 0..curr.len() { + match curr[i] { + LineMaskEntry::Gear => { + let mut set = BTreeSet::new(); + for row in [prev, curr, next] { + for n in [i - 1, i, i + 1] { + if n == 0 || n == row.len() { continue; } + if let LineMaskEntry::Number{tag, num} = row[n] { + set.insert((tag, num)); + } + } + } + if set.len() == 2 { + let mut iter = set.into_iter(); + let x = iter.next().unwrap().1; + let y = iter.next().unwrap().1; + acc += x * y + } + }, + _ => () + } + } + acc + }).sum::(); + println!("Gear ratio sum: {gear_ratio_sum}"); +} -- cgit v1.2.3-54-g00ecf