Day 3 initial solution

This commit is contained in:
pjht 2023-12-03 17:31:26 -06:00
parent 16888bf459
commit 9437441dc7
Signed by: pjht
GPG Key ID: 7B5F6AFBEC7EE78E
4 changed files with 234 additions and 0 deletions

View File

@ -9,3 +9,4 @@ edition = "2021"
aoc-runner = "0.3.0" aoc-runner = "0.3.0"
aoc-runner-derive = "0.3.0" aoc-runner-derive = "0.3.0"
nom = "7.1.3" nom = "7.1.3"
simple-grid = "2.2.1"

140
input/2023/day3.txt Normal file
View File

@ -0,0 +1,140 @@
...................305.124................................432..............................................576..313.....514.................
.............113...-......&....................&...819...........654..../..........................&901................*....869.257.........
...377..&783../.................................9...........855*......940..463................-.........................844.*....@......679.
......*...........197.261.....817..336.759............&742......548.......&........748......844.............#.......&........254...169..*...
.......36....368.*...............*....*.........*..88......%866.......135.........*..................515.682.....114...%...........*.....768
...233......*....909..698.........427..........127.*...................*...........450.........482..../.................312.....621.........
.../.......882...............776...................555......180.........971....217.......857.../........212....674.917......736.....441*760.
............................*.....@....907....%940.....%.......+.#..........45*...........$............#..........*...............=.........
..................370.225.425..211...................932..........381..267.............45.....=....549....238........367..&.....488.........
.468.............*.......................761...677.......&.................929..907.....%....79...*.......*....324..*....515................
....-.#103.504...388...........$..........*.....@........766.%................*....*623.........532..881...573....*..840..............497...
...........*.........305....412..687.......971................676....@640....657........709......................807............98.....+....
....279...502...62...*.............*..............................................838%.....$.........585..........................*.........
....=...=......*....209.825.482.993..........972............620*664..-759............................*.......184.........952....160.....156.
.......814.....931......*...*............551..*....711...65.................224*115...851.772..104..505.....=........796*...................
....................723.103..275........*......564..*.........830........................*...../..............................232...........
.............$......&....................951........703......=............160+.....138......*........257*809.............$298........284....
.....684....464.333..................827.....*211..................................#.....855.86.....................$913............*.......
.......*...........*..*......917.163....*.409......294.......................#..39.......................511....358.......$671.......237....
.......663.......311..163....*...%...158........*....-.442.........958....108.....#.....941*837.........*..........*........................
............865=..........409....................128...*.......283....................................680.........730.935@........768.......
....../................=.............54.....857.........418.....*..911.......610..643............381...............................@........
.......925............984...........-...800*.................194.../.........*.......*.........../.....................................@....
.................................@...............................&......615...16....695.....-..........................*750.............855.
....611..136...................108.540.........362..711......*....420.....................190.......................213.............86......
204*....@.........288..825..+........&...........*....*......733.........................................7..387.......................*.....
...........795..../.......+.505.834.....228....192...619..........886......467.....942.............................%838.........512...340...
.......957.........................*...*..................962........................*..............211...$..............624................
.......*....................349....144......./786..350.......*.......................541..21.......$.....284..190*......*............518....
....484...@....17..........*..../........309..........*....530...118..........208..............253................489.828...........*.......
..........309...........715.....531........+.516#...645.........$........369....*.......*185...@............=.#...........@............&....
491*192..............................................................233.........711.948.........572.....869..289......932...........149....
........274.....511.....425..........421.288*965...652=.919........./....*.786...........676.....*..................+.........=.............
.........*.........@......$...*.........................*........*....791...+..113......*....527..897..499+.904...166........580.........389
.......306.....................353.497..929/.......=....553......495.................884.....&........................794........774........
...................../...........................582....................................................489..........*...............&......
.605......537.....247.....................479................74................227*995.761.....@...........+.......958...87.....547.795.....
...*.....*....................289+.....*.....%........967..............*...............*........611.....28....367.................*.........
..690....916.497....................894.440......547$.............596.492......23...............................*...............237.221..786
.........................681.....=..........580+.................+...............+.......696............@..542..56..........................
...936..639*947.............+...48.*709..................307.257...-248.697.553.............+.........146.....#.......*241..................
...*................568....................=378....41.......*............*...%..11......*..........................982.............#........
..323..704.+724..*....*..195*....812..879...........*.....*....508@.....852....$.....664.......317....*......332..............326.158.......
.......*........610..767.....936...#...&..744.290..839.382.543....................................&.827...../.............323*.........884..
...........962...............................*...................46....502.265.150...................................639...............*....
955*818.......*...@...124*512..256...................+....&..............*....*.......170....996...#........................786.......4.....
..............284.697...........*......172...........398.435.......-.....380..........+..........698....428...361................206........
.822...#..............936$...745..........*..%59..................726...........+.........249......../...*.....*..................%.........
.....870................................978.......761=.......179.............692.........*..........477..550.....676..281.............507...
..............913.................215.........................*..745...#.............882..130..569....................&..........*472..*....
...27..710.....*.....................*.205*403..............59....+..172................=.............................................697...
....*....*....731.......865.......155..........849*....#..............................................679...854....................&........
.985..%.............599....#..........191..520.......473..#........./...........708....406..677+...=............................599.....133.
......501...........*........@.........*...*..............446....767...*444........@......&.........49......#...311*413.....................
..................157.......830..13.90.398.945.978............................950.................%.......88................$...............
.............297........618.......+.*............%............317.............../.........709..481............503.........570...305*845.....
......661....*....632.....@..+......524......../.....+...........*...590..............944....*...............*................+..........681
.......*.....820...@..........829...........671.......206.......239.+......................339..#............106......415..213..........-...
........114........................903.669=............................764...319....373........642...................................-......
.................*...........................*.......121.......898&.....$......*...*.....................................211#.......579.....
........*.......362......787.440..........842.825.....*......................590...357..........949......523....470..292....................
.....594.459........98.....*....*.......................355&..*..................*..........554..*.......*.......*....*..222...243..........
.........................535.....674.........804....#..........455.............847............=.32...23..951.....217.346....+.*....+272.....
..183...........443..........119.......833....#..693......818#........................@..699..............................*....678..........
...*......455/...%..826.............#.....*.........................$...........868.739....=..753...181....773.........323.508..............
...382................*...169..510..590.295....212.767....*970......711.........*........#.......*..*............651...........36..766*.....
............&..61..697.....*................46.*....*..691......970............786....953.....775...235..................*550....*.....417..
..........45.....*........689...645.....549*...821.601.............*......................952..............#..........464.....609...........
.....657.........48...888.......*...................................305......+47..........................254.27......................123...
818......388............*....412....../491.........#336...506..422.................989....15...70..............*..354............@.....*....
..............506........11......./...........643..........#..=..........703...963*........@.........613.....413....%...485.......265...33..
....589.......*................207..............%...............................................+................*......*.....94............
...*.........663...413+....................................819...........655.%...648..823.......770....385......457..150...#.../............
515.......................475....825......33../350.....459...%..........*....681...*...*..............%..................437.......689......
....$993..897@..............*.......*837...-...........*..............726........372....922......&198................................*......
180............*..........679.................71.629=.796......109...........................527..................682.....558.....798.......
........437.742.46.................@.......&...................*......498.......@.............*............./......$........*...............
..761..$...........148.......856....701.377..536*510...604...540.....&........929........172.799.............310.....700...........440.%....
.....*........808*....=.........@......................*.........577.....................*................&...........$...417.39.....&.229..
........794.......446..............................214.543...%...*..........935......865..445...../....%.615................=..*............
..572..+...............................419....932@.........924..72.902.......-..579.-...........997.203................873......945..567....
.....*.................273.............*...........................*...142............688................755.850...........846.........=....
...880................*..............883.698.....$.......=........197.*...........316*.........365.........$...*............*............904
.........385..977..179....+...................652......875.............350...863............73......464.........214.852..478................
......22....*..@.........93............*682..................*137..309.........*....557.....*........................=.......698.....+.218..
......*...645......163...............89......*853..................*...386..341.......*...228............718*492...............*..153..*....
.....576......................857.........513....................509......*.....322.710.........615.......................613.165.......292.
................................@.....370.......598......487...........503..*.....................*...738#..#..$901...$.....#.......648.....
..19*454.............78................/..876..#..........*......+..........937.......383........523........69......326.678.....212....*....
.....................*.....436.@...646...*..........100....367..325.....361.......207*....50............621...............*..........237....
757.........714......502...%...54......909.411.....*.....................*..........................=..$................415.................
...$.......&.................................*...471..985...508.472....891.842..$.................418.........&.....&.......................
.................$........906..........*215.338...............*..............*..606.#235......................813..259........229...........
........472...222.........*.....245.572.......................927...769....344.............291.239.....................94....*..............
...818...$.............291..............288...........163.734......+...........699............*.......981&...514*.............224.....=.....
.....*..................................*.........................................+..189..........452............644...................669..
....850................326$..............869..&985......784..../..............592.......*..153.....*....887..493.....211........%...........
.............................457....834.....................94..564..........&.........498....*...304....../...*.....*..........845.....&...
.......12*48.753...244..196....=......$..721....90*29.........*.................+.522.......487...............317.....531..311........20....
.177...............*.....*.................*.............*969.611.......*565..338...*.............712..922$.......770......*....522.........
....*..459......149......165...=........879...........103............497..........258.931..75....*..........400*.....*......972.............
.269...*............#........715.............................*475..........................*....145.............414.....228........893......
........209......508...................../331......469....894............347.890$..541.....56...............#........#..*...................
......................=...........37................*...........................................$...........197...119.....=...........756...
...........&.......850.....367.34...*............517........178*716..........=...................892......-............883...755............
.....@496.125.................*......965.....428.....908..................405.....650...................=..493..89..........................
..............581........396.............596.........*....964........672......159.%....463............768......./.............391...........
.............*...........*.....871.660..*......577.73......@.....932....#....*............*574...186........154......996........*......720..
............914..132..137......=.......746......*..................*......376.......&661............*560.%.......%....*...264....511........
...%...151................*851...................................*..284.........754..........190.19.......611...326.841...............89....
644.....*...797........133.....476..............................810.............*.....866......+.*..............................#903..*.....
........839..*..478...........*........372.#.....744.....227*............477.3.253.%....&..746.....289.&860.....720*708.....................
............443...%.....68*....283.......%..900...%....%..........584......*.$......308....*......*......................592................
...239................@....250........50........&...%...192.$111.*........74............831....914...#.....886*492..........=...............
...*......425.....181..680..............*......873.502............93.................................503.@..............116........372......
492..........*.....$.............302...789.................861.......................290.......452........83....626....*.....76.......*.....
......178...528..........*739...@..........................*......804....320....#.........&878.*.....746........./..&...148...$......741....
...&....*.............262..............325...........599..592.579.....%...+..808...............386..*...............761.....................
663..462...........*..........109..706*.................=.............553........712......*971......674.396...635*..........................
........../...728.952...413......*......744.......%..........................300..*....782................*.......742..&424........41+...564
........375...%.........*......450.456.$.........714........851.327..#...+......*...+.......179.630....854..................................
..251................195...887.....*........*209.......430.*......-.83.596....956..966.274.%.....*........................83.359........411.
....%.......................*..........89.........194..+...491.........................&.....*.762..+741..........598.......*.....910..*....
..........618+..............677........*..598........+..........-.....58....290...........795...........................*.....580.&....559..
...................561..............910......-..321....834....602.752*....=..+........191..............463....@..........792..*.............
............216.......%...722*668.....................$..................109............*....222...195*.......689....380.......637..........
.....846.......*..436..............=827...263@.764*.......*.464.................729.....934...*......................................289+...
.127*.......757.....&......./954...................965..664........./......716*.+...........266....781.........989..........844...$.........
................57.....................&118......=...........385.623...245..............345........*.....615-.....*..........*...768........
...538...........#..860......................783.460..........*............401.............*796..354..............37......797........725.193
....*..#620../.........*..........578..54...*..........780..251...634..946....*........................./....702........................*...
..202.......815........724....#..............822..972...................$..........*.....#....@........27.....*...........268.....347.......
.................+..........98.....703..............&....*52.....320.............670...318....734.............47..................+....239..
.953*919...=......273................=...454.....@....601...........*...958.........................+.................758...=...............
..........905./50.........@...................971..................762.*..................169........915.682..........=......533.......502..
.....884.................24...........262.........@.....531..698.......234.................................................%.......149..*...
....................834.....994.......*........266.....*......*............................................/.....941.434...812.......*..211.
.............133......*......*.....613...85..........871.......497..346...737....88.....176.....192$........128.*.......&......./163.26.....
....734..543....*.....656....461........*......531..................*........../...*..&................$971.....931.........................
................606....................506............................779.......30...211.....243..........................153...504.........

92
src/day03.rs Normal file
View File

@ -0,0 +1,92 @@
use std::collections::{HashMap, HashSet};
use aoc_runner_derive::{aoc, aoc_generator};
use nom::{Finish, IResult};
use simple_grid::GridIndex;
fn parse_num(input: &str) -> IResult<&str, u32> {
nom::character::complete::u32(input)
}
#[aoc_generator(day3)]
fn input_generator(input: &str) -> Vec<String> {
input.split('\n').map(|line| line.to_string()).collect()
}
#[aoc(day3, part1)]
fn part1(input: &[String]) -> u32 {
let mut symbol_neighbors = HashSet::new();
for (row, line) in input.iter().enumerate() {
for (col, ch) in line.chars().enumerate() {
if !ch.is_ascii_digit() && ch != '.' {
symbol_neighbors.extend(GridIndex::new(col, row).neighbors());
}
}
}
let mut part_sum = 0;
for (row, line) in input.iter().enumerate() {
let mut col: usize = 0;
while col < line.len() {
let ch = line.chars().nth(col).unwrap();
if ch.is_ascii_digit() {
let rest = &line[col..];
let (rem, num) = parse_num(rest).finish().unwrap();
let num_len = rest.len() - rem.len();
for i in 0..num_len {
if symbol_neighbors.contains(&GridIndex::new(col + i, row)) {
part_sum += num;
break;
}
}
col += num_len;
} else {
col += 1;
}
}
}
part_sum
}
#[aoc(day3, part2)]
fn part2(input: &[String]) -> u32 {
let mut gear_locs = Vec::new();
for (row, line) in input.iter().enumerate() {
for (col, ch) in line.chars().enumerate() {
if ch == '*' {
gear_locs.push(GridIndex::new(col, row));
}
}
}
let mut gear_neighbor_numbers = HashMap::<usize, Vec<u32>>::new();
for (row, line) in input.iter().enumerate() {
let mut col: usize = 0;
while col < line.len() {
let ch = line.chars().nth(col).unwrap();
if ch.is_ascii_digit() {
let rest = &line[col..];
let (rem, num) = parse_num(rest).finish().unwrap();
let num_len = rest.len() - rem.len();
for (gear_num, gear_loc) in gear_locs.iter().enumerate() {
let neighbors = gear_loc.neighbors().collect::<Vec<_>>();
for col in col..(col + num_len) {
if neighbors.contains(&GridIndex::new(col, row)) {
gear_neighbor_numbers.entry(gear_num).or_default().push(num);
break;
}
}
}
col += num_len;
} else {
col += 1;
}
}
}
let mut gear_ratio_sum = 0;
for (_, number_neighbors) in gear_neighbor_numbers.iter() {
if number_neighbors.len() != 2 {
continue;
}
gear_ratio_sum += number_neighbors[0] * number_neighbors[1];
}
gear_ratio_sum
}

View File

@ -2,5 +2,6 @@ use aoc_runner_derive::aoc_lib;
mod day01; mod day01;
mod day02; mod day02;
mod day03;
aoc_lib! { year = 2023 } aoc_lib! { year = 2023 }