From d5ac973448e8ebaf080fb929eb5034c997ada5a0 Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Sun, 3 Dec 2023 21:01:49 -0800 Subject: [PATCH] Day 3 complete. --- input/day03.txt | 140 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 129 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 269 insertions(+) create mode 100644 input/day03.txt diff --git a/input/day03.txt b/input/day03.txt new file mode 100644 index 0000000..49321d0 --- /dev/null +++ b/input/day03.txt @@ -0,0 +1,140 @@ +......124..................418.......587......770...........672.................564............................438..........512......653.... +665/...*......................*599.....*.983......794*..140..*...........@..963*....................445........*......*.........709.....*... +.......246.....581......701..........108....%.532........../.73..699...927............................*....579.354.464..............298..86. +........................*.....@...............%........$............+.........167..................408............................$..*...... +....914......335.......513..245....106=...............974................749.....*.702.......589........803........*176..386.....631..340... +....*.......*...........................48...203@.............767......*..../.362...$......................*159.381......................... +..620.....430.....612.507.........365.....................335../........938.................154.........@....................682............ +.....................*...........*..................470.........................889...........*.....@.489.....453$.329..334............86... +..........324...............431..58..533-../..-...../......*405.................$.............47..474..............*......*.......930.*..... +............/.....*350....................400.502...............$...........168.......855.635....................258.......794...+.....846.. +........................560...72.945..............866..........783..328....*....116......*...........179..904........682$..........333...... +.....674...........152....*....*....*815.........*........$609.............737../................583*........*.84..............767*......... +..../......55@....+........645.914................987..................*..........972.........#.......80..750........588................=260 +.....................349&...................../.................754.407..203*720./.......207...14...............=88...+...767............... +.........*824.............890.......269....893..271*139..645....*...................233...%................428...........*.........79....... +..........................#............*.................@.../...316...844.............*........@439...287*......*974.....182............... +....*.....50.......671+.................267........634*....417............-.598.....531....891................331................358.....341 +.883.561..*....428.........../14...742...........@.....654.....809../716.......*456.....=....*........$..............................607.... +...........835..*..796*............*..............321......612*.......................299..203....962..431..........277.......40......$..... +......+591.....916.....294.........446..111......................237*77.....&........................-................*...150*....*......873 +....%..................................*.....................819............522.................922................738.........214.595..&... +...552.........*...............+545.....627...........601......*..801..............867.....954....*.984.....752..........*830............... +............276...939.............................979....*.........*........866*.....*.......*..963.-.................172................... +278................*......@..........52........51*....851..766@..515.949=.......736...456...107............796.@668..................#...... +...*...431.................616..............................................79..................651.806.....%.............554.........740... +.969...-............................-..........721.......555.657....+.........*....#....704........*.................556...*....196......... +................*228..........312.201.....490...%.........*..#...815........896..417.....=....890.....274....884.683*.......327...*......... +......@......742.............*........393*....=........715..............535...................*.........................529.....741.....#... +.......386........244......196...............815...........869....+580...*...................71........654...454..346=.$.............909.... +....................................................265...$.............738..401...984...........265.....*......*...............-........... +.......=.....184....148....14.........685...990..................80..=....../........$.......511....%.....424..400......184..551............ +.....71......*.......@............812.........&..$.....573*613.....*.677.......#.......299..&.......................933..................... +...=.......142...917...-989....76*....230...*....105.............920.....+......371.......*............660..692.........553*........%....... +341................&.....................=.29.............643.82......*...714............222....934......*....@.............178..581..+..... +...............874...129.......................739*971.......*.......176.............3.@...........*..219..40..........#.............168.... +.....179..............*...741.......524...................................757.=633..*...136......63........*..........399................... +.......*....315....307....*.....%..*.............718......371....=....654*..........89...................194........................+.23.... +.....737.........&........540.253...80...273......*.........&...773............492.................722........113....970..=195....702....... +..............869..........................*.539...434...............393*933......*........679.874..%......=....*........................... +............................$....148......43...*............@....................67.876........=......./..388..920......423.........-....... +..118*773.142....%565.......397....*.........352..#..........217......................*....865......257..................*........421.415... +............&.........898.............607..........897...631.................787...840.......*..............684.........34.359.........*.... +........995....235.....@.........#.......*.932*.............*73...940..997..#..........&.&...942...$.727..........115.........*122....380... +................-................131...........265...827...........*....*............797.490.....845..*..........#....+823.................. +............96.......................383.......................53.292....19...536......................42..668..................579......666 +...-..487............680..&...*45...&........801.............*.*.................-........374.....................128..109.......%....*..... +.611.+..............*.....151................=......739....622..572................103...........683.....245..748*....*............298.67... +........*735......911...................562........@.........................458.....*...753........................275.362*................ +.....683......702.....736.230.....457.........................13................*..126...........458........................890.........992. +...............*......*..........*.......499........10.........+...227.227...542..........................167....661....................*... +.............691.7....135.62..157..570..*....304......*...........*......*................$589....#946....*.........*............132....190. +........687.......*.........*............768..-...453..643.........844....706...%......................509........767......*................ +...........*....485......859...........*.........................................92...268.........193.....................385.....991*722... +...-.....18..................217......853................................28..............*........@.......+......302........................ +...103......60=..*352...........*916........351.....347..=..452.810....................304..........539.346.......*....................*.... +......................610..........................=....990.....=...819....*496..797.................*........946..44..................261.. +.......630.............*........882.........................173....*...............*....903....44.....318.212.@.......-..................... +.......-......-...........@..=.....*841.....812.......515......*....713....+.....566....*...................*......344..297....356.430..%... +.........*482..453.......279.554./............#..320..*.....................671......873........&...637$.....413........@.........*.....906. +....263...........................861......*....*.....908......365....123.......494.............134.................808.......*.....*....... +..........465.....520%.....................432.76..........160........*......................26.......218....14.......*......598.874.844.... +...417......%.............138.....................$..............84..............272...573.....*.296.*................585................... +...*................596...&......................783.....992..........*....982.....*.........857..*...314...797..265........*....*..547.#... +....260........75....*...........389....616=.........5.....*.....695..427..*....780....-425......872..........*...*.......49....599.....19.. +..............+.......389...........*33.........596.......600......*......67......................................567....................... +...802............................$.....302....*.....-93........434............$........554../339..............................277.......... +.....*....................$.....822.....*.....89...........233..........602.....911.....#..........958............475......773*.........%989 +..849.............228..868..217......679.......................99*...../............................*....................................... +.........253.........*......*....................643..............796......-200...355..469.........174......=........174....279.638......... +295*22..*............664...462.-238...................&765.........................*........................241..............$.......%...... +.......937.....25....................422.264.................244...........628...340..................106.........................551.....82 +897*.........../..60.......361.......*......#.....164.........*.......804*....%..........670*194........#.......#..........83............... +....754............*.................51..47.......*.........487...585.....202........838..................28...734...*457..............427.. +............155...705......................*....912.....887..........................*....*875...........*........................&......... +..952.621......*.............+983...........832.....783....@.......432#...+........530.223.............456......462...257*100......763...... +..........=..905........................892...............................262..85..........#..................-../.....................671.. +.........994...............476.............%.665...524...53*........939*........*..........703..497.........186............=.567........*... +154...............$412......&..........-.....&.....-........41.109......282......676...........%....&80.@...............439...*.....@...997. +........+130..................296.....308................@.....*...902..................................77....833.....%.....932.102.48...... +................407%.%685......*..........927.=222........426.450...*........12.....82...../..570................*...798..........*......... +..825.....................923.429......#..*........#630...........409..314*..........*...569.....*....273..648...961.............279........ +...=..827=..........293.....#.........875.401.............457.433..........690.....600............929......*..............*245.......93..94. +.............557.......*.......................52........@..........284.......................450......................986..........*....... +........................463.....583..708...........................+........101....834.445.......*.....336...................694...333...... +....556....923................./......*..433.....182....181.........................*..*......492..598.*............260.....*............... +......*.....*....430...............960....*...+..&.......*..=96.....628.282...@355.833.883...........*..468...420...........288............. +...140...682......*..945...150............7.654....+83.941...........*......@................#....7.780.......*......922.........334........ +.............365..49.......$...590............................608.........503......./......36.....*.......74...340.....*.................... +..........@..*.......870.-.....*...284*556................288..*...@.............$.526............587....*...........387.................... +.......188....626.$......372.733.............................*..97.466.....776.541......................413.950..........696.162............ +...../.............755...........62......99...............224...............-.........333......................*..337.....*....#......%233.. +442..7.......=...................#...........875*705.548..............963...............*......180.....581...350.....*....513............... +..............377....................................*.........#......*.........291......981..*...........*..........74..............&...... +......895.........644...................613.......540...........756.............%............390.263....754........#.....713-.....450....... +...85*....$.............297......%.........*..........156............974./870....................*...........12....426.................483.. +.......774............#..*.....872.............361......-...196..................849...........419............*.........221....667.......... +...................961....604...........644....*...............*........../927......*......503................124........-......../...238... +....370.............................531*......174.693.........349..................495.......*....925....................................... +.....+...104....582...602*604...123..................*..896..........71@.....................767...............573@....656......%........... +140........*...@...............*.........-........592..*....................806..692.....511...............755..........*......917...636.... +..........905.......462.....+...655......860..150.....800..903.......8*920..........*867.@...................=..........269................. +....582..................217........842.........*...........*..................912...............551...615.......-..........343*129....+.... +....*........................./.....................752..347...275...127.@........*.....804.....#....+.#.........252...845............671... +..862..........38..........293....429...@............*.......*...#.....*..245....330.......*18.....182.....$538..........&..725............. +.................=.............46*....696.............581.664.........608...........................................%.......=............... +...*724.....977.............................../................565...............#15...............................782.........359....$..... +529.....373.*............198.983.....980..559..592...100.579......*889..145..839..............790.....496....193......................545... +.........*..560..769.......*.....775...*.............*....@..104................*...%..820.....*......*.....*.............245...160......... +.....338............@......28.......*...725.687....761......%...............%..423.773..*.....315....765..69.................*...*.......... +.851*.....619...333..............907..........*.........512....536..........98.........434....................48.613.973..941...554..751.... +....................*564....122.............608........$.........-.........................48.........435.......*.......*............&...... +...........165*967...........-..799.186.938......@.615...317........................630...........551*................345................... +....968............281.546.......*.....*.......225.......*...162....372.........&85...+...177@...........719..55................360*........ +.......*.......%.......#.......226....................341.....*......*.............................341.........*...........989&.....321..618 +....884..554.163...%...............*423.233.................653...557....$....910...................*...........109....772.................. +..................854......167..560......@.....311...958..............492.....*.....*877.........@...911..797%...........*.................. +....852.815.....9.................................*....#...../..941.........960..494........56.375.....................50..+......=...106... +....*......*461.&.......739....$..........=.$562.276.......408.....=.....................=...%.................769.........893..463..*...... +.133..................-........797.....313...........................................20.214.....357..776.471......*687.@.............527.... +.......14........899...845..........*..........+.....46........634........914.....84../...............*.....*780.......878..%36.435......... +542.....*...........$........*833...257..329-.147...........+........150..*......*.......907...........429.........................*....#... +.....517..799.44@.........230..........................477.579...........836......839.....-.....................964.704.............194..310 +298.......*....................307..800......346.65.....*..........414........@.......619........&442.............*..*...................... +...........510...+.....837.237*.........../.....*.....270.....818$..+..........27....*.................163.....140....647....764.163........ +................181...*..........536#....335......................................610..170...............*.-....................*.......&... +.......................832........................#........&........611.........................&55...428..472....586......111........768... +.......763.....................................461.........381......-............566..814.....*.....................*........./............8 +.............+.......................318............695............................=..%....323.756............711..663............827....... +...........526....=....675...353&.....@..982#......*..................+.62...880+..................631.......$......................-.118... +..................655....*....................795..30...922*.......978...+.&.........539...........-....719.................599............. +......2...574%..................#.698...475.....*...........652./...........464.163$...*..338*966.........................../.....534..386.. +......*.......................404..#............747...703........231...-...............................................................*.... +.......906.................................&575.........&....457.......633...395..761...355.#780....3+......799+...............496...264.... +311............967.682............%.838...........253..........@..551.......@....*.....*.............................487.........*.......... +..........@.......*......925....376....&...419......=.............*..............20..952.111/....648.&........748................834..706... +..443....940.............*....................*..........................820.684...................@.755........*........106.283............ +..............397.........803...84............627..........704.983..........*................522............................*....541........ +.....32....$.....#...643*..............116........./905......*..../...........311......811$.*........*890..........924..670........=....882. +......*.....81.....*.....636.......317...*...................899.............*....*698............626....................-..+..@.......*.... +.......877......256.714...................825.........458....................869..............................54............28.823..110..... diff --git a/src/main.rs b/src/main.rs index 56e6085..8076893 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use std::env; use std::io; @@ -152,6 +153,132 @@ fn get_possible_games() -> (u32, u32) { (sum, power_sum) } +fn record_gear(num: u32, gear: usize, gears: &mut HashMap>) { + println!("Gear {}", gear); + match gears.get_mut(&gear) { + None => { + let mut vec: Vec = Vec::new(); + vec.push(num); + gears.insert(gear, vec); + } + Some(v) => v.push(num), + } +} + +fn is_part_number( + r: usize, + c: usize, + s: usize, + part_table: &Vec, + n: u32, + mut gears: &mut HashMap>, +) -> bool { + let is_symbol = |c: char| c != '.' && !c.is_numeric(); + // Check above. + if r > 0 { + let mut c_: usize = if c > 0 { c - 1 } else { c }; + let row: &Vec = &part_table[r - 1].chars().collect(); + while c_ <= c + s && c_ < row.len() { + if is_symbol(row[c_]) { + if row[c_] == '*' { + let index = (r - 1) * row.len() + c_; + record_gear(n, index, &mut gears); + } + return true; + } + c_ += 1; + } + } + + // Check Adjacent. + let adj_row: &Vec = &part_table[r].chars().collect(); + if c > 0 { + if is_symbol(adj_row[c - 1]) { + if adj_row[c - 1] == '*' { + let index = r * adj_row.len() + c - 1; + record_gear(n, index, &mut gears); + } + return true; + } + } + if c + s < adj_row.len() { + if is_symbol(adj_row[c + s]) { + if adj_row[c + s] == '*' { + let index = r * adj_row.len() + c + s; + record_gear(n, index, &mut gears); + } + return true; + } + } + + // Check below. + if r + 1 < part_table.len() { + let mut c_: usize = if c > 0 { c - 1 } else { c }; + let row: &Vec = &part_table[r + 1].chars().collect(); + while c_ <= c + s && c_ < row.len() { + if is_symbol(row[c_]) { + if row[c_] == '*' { + let index = (r + 1) * row.len() + c_; + record_gear(n, index, &mut gears); + } + return true; + } + c_ += 1; + } + } + + false +} + +fn sum_part_numbers() -> (u32, u32) { + let mut part_table: Vec = Vec::new(); + let mut buffer: String = String::new(); + while io::stdin().read_line(&mut buffer).unwrap() > 0 { + part_table.push(buffer.trim_end().to_string()); + buffer = String::new(); + } + + let mut sum = 0; + let mut gear_table = HashMap::new(); + + for r in 0..part_table.len() { + let mut c = 0; + let mut part_rem = &part_table[r][..]; + loop { + match part_rem.find(char::is_numeric) { + None => { + break; + } + Some(ind) => { + part_rem = &part_rem[ind..]; + c += ind; + let is_non_num = |c: char| !c.is_numeric(); + let end = match part_rem.find(is_non_num) { + None => part_rem.len(), + Some(i) => i, + }; + let num: u32 = part_rem[..end].parse().expect("Couldn't parse number"); + if is_part_number(r, c, end, &part_table, num, &mut gear_table) { + sum += num; + println!("{} {}", num, gear_table.len()); + } + part_rem = &part_rem[end..]; + c += end; + } + } + } + } + + let mut gear_sum = 0; + for (_, vec) in gear_table { + if vec.len() == 2 { + gear_sum += vec[0] * vec[1]; + } + } + + (sum, gear_sum) +} + fn main() { let args: Vec = env::args().collect(); if args.len() < 2 { @@ -163,6 +290,8 @@ fn main() { "day1b" => println!("Sum: {}", get_calibration_digits_alnum()), "day2a" => println!("Sum: {}", get_possible_games().0), "day2b" => println!("Sum: {}", get_possible_games().1), + "day3a" => println!("Sum: {}", sum_part_numbers().0), + "day3b" => println!("Sum: {}", sum_part_numbers().1), _ => println!("Unrecognized day: {}", args[1]), } }