Compare commits

..

2 commits

Author SHA1 Message Date
dbd12f59f1
day 3 part 2 2023-12-03 17:07:13 +01:00
2e0c80df80
day 3 part 1 2023-12-03 17:00:03 +01:00
3 changed files with 260 additions and 0 deletions

140
inputs/day3.txt Normal file
View file

@ -0,0 +1,140 @@
..975..95..................717..........................................747................................................622..............
................/47...........@....701...610.........252.660*.............*..236.....323..........108........653............................
.......69............313..............$...$...*....@.........640........81.....*................................*332..92....................
........................*...210.............767...912...367.......505.......817..................*.........478.........=....223.....568.....
..76...968............108...@.....556.....................=..........*...............412..313...575......../...........................*107.
............773/..891............*....................744.....805...14................../..../................320&.567..#...................
.962..708............&........399....146.....385.................*..........825.......................................-..655....485...-.....
...*.........+..........................*76...+..................242....997..*......185..........207.390..870...883............*.......337..
....929...299....................*.................249........-...........*.373.....*.............*.....*.*....*.............549............
................196.745.996...364..............409...-.926.....273.....580.........31...........545..547...862......810.....................
................*.......*..............900+....*..........*........586.........936...................................*.........673..........
.../..........864.683.215....*......%.........702..166..46..........&....341..........694..496.......528.%...........839...105..*...887.....
....506...617.....*........51.807....942..424...............................*..........@..+............@.84...................*......=......
...........@....594.........................*..........*675...........345..52...$..................-..............899.........804.$.........
.362*366.................487+.48&.......600..180....845........................535...18...........397.#908....432*........229......346......
.................*................835....................578......*502...............*..416.............................................391.
....610&...514...501......./........*....*71.........../.......674......925...............*.869.....966........393................/.687.*...
............*...............786...248.104........92.633.....%..............*104...&466..138..@.........*.525...+......974......339..=...260.
....372/.889..............................55.406........134..250.=228..609...........................69...............*.....................
...........................................*........976...*...........=.......357...........................191.....150.....................
...-...........595.634.........*396......592..594......*......411.856...760....../..................201.....#...709................168......
.936..236.........*....@....436.....100....../......35...../.....*........*....................917%............*.....................*..#63.
.......*..............542..........................*........689.........26....667.&...................738....831..$.............513.53......
........10.....529........*250..........162.......390...........................*..70.............................701..........*............
..355.........&........394......301@..........542..............................142..........%..............%..........*.....507.............
.....*.........................................*.....%..283.....*.........997&.......@670....402..........808......308.459.......712...152..
..291......246.@....+986.....336/.......976...468.264...*....739.187.............................................................*.....*....
........../....46...................186.$..............674............................227......-............975...............436.......492.
.491........................810.......*........................772*.........742........%....198.....*702.......*.596=....64.......=.........
...................706......=......415..................=.199......235......*...47........................72.607...................443......
.214..............*.....771.............302...........230..+..............810.....*219.606........912.....................=.................
......277......320.............937.....*.....+....632...........&374..............................*....................898.....642%.........
.........#....................*.......716....229./.........................$................934...233...............98............../.......
.....*........-659..457.........964...................659....786....619.666........277...@.....*......930*460..618...........910..265.......
..845.877.343......................*537.../120.......*.......*.....*.................&...175..631.............=.................#...........
............*....................+................@...35.949..34.381......273...880......................451........326.....................
............876..880......827..733.......645.......38......*.........402.....+..........902....459.775........837.....*.185.......813.......
........578................/............*...............622..18.........*234..............+....%..........448.......935....#.............350
..........*...468.....546................588....146=.........*..142.795..............*424................../.....................=..........
........245............@...746.......362..................970......*..............593...............487.......+.............709..593........
...990......&415..............*381......*.....................435.......214..908..................*....*.....351.....876.25....*............
......*............293*.....%...........271.............135......*......=........540............177....604...........&.....-.46..........166
...609.....................910.................@........$.......321..........141....+.45.....................750......................98....
.......#....779........944.....653...........797...........788.................*......+....164.724....475&...+.............-.471......&.....
..794...487....&..81...........*...................@............666@.....*981.937.............*.....%..........888....+...81................
.....*...........+......694.............748...104..859..............................675.434.......290.............*..352........353=..$.....
...619.977.........859..#....235....230*......*..............429........335....-.......*......631.......=.......27...................192....
.......*............=...........&.........121..680.195.959......*417.....*..938.............../..........739..#..............194.750........
....309...439.............14...............*..........*...............723.........163..748...........853.....885.........175*........487....
.............*707..-626..........464.....382................356...........868#....*...*.....13......*............450............#.....*.....
........216......................*...619......$..270........*.....892............25.494.841*.....971.............*............157.+...119...
...#40...../............343..559.425.......536........718....739...#.......284...............726.....985..136...597.621.173.......203.......
................383............/......................*...................@....348............*.....*.......*........./...*...811.......*158
...=..............$......@....................776.829.85...&........541.......*....*...&....700......341.995.....73......114....*....267....
.700.203....591.*.....924..........$217.......@.....*.......590....*.........812.579....517.......................*.............730.........
......#......*...586.........312..................806............435...259...........................573........453.282*132.243.............
..........224.................#.......468@............-...%.............*..............453...................$.....................-........
....................................................227.673....881......828..&.....632*...................589.....................284..-....
.....680.........871....17+................117.................*.............506..................#...410...................616.........519.
........*..386................78*762.........@........28..557.655.......359........-............124..#......&..........843./................
......724...*..584.....552............................*...&.................209..857...633-..............602...932......*........132&.......
..........41../.........*..........................#..427.............=370....*................................*...353...902...........529..
.....................150.......868.......370....549.........779...................284...........431.....3.....889.............189..103.=....
.......530..355..........442...*............*63............+.......852.....935.....*..............%.....*..................@....*.-.........
..........*...*...976.....*...500....@...........572...........940....*990..*....627.790.................460..904..601-..821..930....699....
..........643.824.......248........358...4.........&..301.........&........538.........*.%436....565..........*......................../....
.768..$.....................186..........*...178........*.......................*.&520..............%........782..........497.-......*......
......762..828*...972......*..............3.*.......................@....../..836...............278..............666......*...927.413.185...
....&..........40.......86.122..........&....450....................1....202............42...51....*..............*.....215.................
.353.................94*.................995.....655.....520...828....*...........612..........&.119...........486...............713........
.........................255........................*.......+.....-..463..........*........235........836....................668............
...................................................286........338........=.393....398........*....199./............631..........*631........
................64%....704...928.........470...125............*....708.15...*.............193.......*........641....*......687..............
........$..............*...................&.....*..........582......*....652.@358..643..........125.....458........168....$.......$194.....
........293.........305......417...............117....................612............*......262.............*903............................
.....$...................615*..........581.........335...139..505.419.............656.........+.......506................710.....956........
776..110.....+............................-........#..........*....*...447......*......................*...*.....938.366*.......#....376....
..........366............844....305..761......780......560........236........725.272.....450.............67.126.....................*.......
.................669$.......$.......=............*220./.....+665........495................*.@103....992....................&........600....
.......*......................812/.........................................$............940...........+...................146...975.........
....410.650.282.....*...............696.833....957...............996...*......772.287...................921..935................%.....$.....
............*....413.921...713....................%.....769.........@.390.216..#.....@..........................=...........*.......46......
.....798.136..............%....150.....236..............*......31...........*..........@.64..17......616..........901......344..............
..............................-........*.....402......873.......*......692...592....390...*.............*277................................
......539.....799.../.....272....%.@....694.#.............667...318.......=...............762.......71%.......-153......................531.
..........556*......573.......465...916...................*..........602.........................=.......145...........................*....
........................451.............../....%.....625.401.........*........428-.....493......479.427....$.........60......951......281...
..947+....36....270....*............517....521..258.*............108..812.............*................*........................*372........
...........*....*....957.823...290.....*.............415..........*...............908..730..........589......+..............................
857.........843............*........794......................677..27........................121...........670.........548.837*....&.....664.
......175..........-.....=........................422.......*..................226-..344.......*...................2....%..........474......
...................283..190..@...........458........*.162@.666.517....202...............*..@...545.%.........705.............*..............
...............*............296.466.....#.........................*..*.....852..208...999.475......256..........=....642..188.386...........
............504.888..................52....416......394...349.....53.953...*......*...........@521.........213........*.....................
......225...............272*32........*.....*..........*...%..............728.........827.513..........287*.........924..388%.........&.....
..453...*..................................71.....*..437.......501...869.......928....%...#....................%225.............734@..157...
....*..400...........810=.300@...13.15.537.....997........./......*.....*.........+.........407.758...674............604....500.............
..970..........409...............*...*...*................159...788....338..............636*...../....#.......#895....*......./.............
..................................52.....872........+...........................-........................524.........297............314.....
...........=...469.....408.............&.........641......364%.780...526.........540.865....742............*..............549..505...*......
........404.......*...-....58..%....577...............655......*.............613......*.............343..779./854............*....*...567...
...103............284......./...999....................=.....640.....284..............566.982..........$...........476...478.793..556.......
......*.....436........916.................941.............@.........&...........284.........*183.........857.953...........................
...............*.404=.....*910..703@...20......266......815.....201.....54.332...........................@......*...........95..............
.............898........................*........*..........13.....*806...*.......280.........................905........63*................
........&.........642..271.627..920.+....534...96..780......@....=...........246../......99........................................../......
....$....518.......@......*......*..323..............-............477....*4...*...........*....979......810.................366....697......
.....439........................364.........161..594.....647..................802.........234..............*17......125..82....*............
................187....667....................*..#.......*.......*................604..................946.............*....414.............
....585...218.....*...*.............-....536.396........950...200......296*935...*..........#...382..........883......182.......326....*....
.....+......-...557.349...........217............................./105..........527..387....394...*...........#..430*.....501.......393.190.
.......*474.............16....................675.....999..............362..............*..........576...................*..................
....449.....+.296*468.....................607....*...........-..#.................619....177...............31...........194..198..187.......
.........156......................330.......+..332...631...936...477................*...........658..........%..845..........*..............
....*262.........$......362......*.....429=..............................*324....296...........+.......161.....*....57.....309..............
.403............492..97.@.....695..696......$.840..&.................21.....................@.........*.......178...+....$.............364..
......178..692*.....%.....82..........=...176.......456.....517........+...238.704.......682......266.................&..222..........*.....
......*........993........*....931*...........445..............*.................=...........@...*.......448.......251................222...
.......824............467..232.......664..299*.......322*512..850..............@..........585....259........$..................&............
...458.....572.................379....*........123.....................-........298..................-................336...908....*........
....&......*....&..732...701/....*....440.....$...........739..636.....65..../.......%151.............823.455............&..........119.....
........550....391..%.............259....................#....*............834............232.179............*.........4...795.784..........
.............%...............729...............568.............936...............952........*..*..............726..........*.........-......
...627.....87....@...........-...........6.....%...106.......................458*.....281..811..282..500..............=....833....214..780..
..............320................665..............*........584.........................*............................343...............*.....
..................997....84.590.....*795.90*487.842..%431....*.......233..692....259....391.113*........*338............./..391.............
.....62.....#689.*.........*.................................444.......*...*......*.............445..861...........498/.857..*.....505*.....
......*...........436.869.....634@..663...599..901*678.100..........666....301..493.986....174.............838*492..........454........313..
.....362.....983....../................+..*.............=.............................*....*.......259.............+....464.........27......
...%........................580....68.....45.........................................21..768...267....*.+....661..879..@....&......$........
.643....545......./435.....*.........+.............................823....919.......................20..102.*..............267.690.....55...
.........*...............557....247.......653....584.5*....430....*........../...529+...........856..........305.254..212.............*.....
........945...528................/...775..+............204...-.537....$..528................276*...................%....*....863......612...
....572.......*.........614.........+................................810...............842........438.....26...&.....%.........*............
.......*....201.@......*....671..$........................../.............................*...832*.......*....214.899..........795.421+.....
994..797.........684.390....*....452../..................989..728............685....23+.856...........498.............443...................
.....................................424.......%...............@.......228.....+..............+.......................$.....................
.....2...812.517.........602*967.........262....953...*.....*....67...*...........+......1.431.....721.....402%..&.......*.....504..........
.............*....%..................963*...........96...287.255.*...548.........534....*.............*..........896..531.789.=.......855...
...........579....595...........8................................781...............................547......................................

110
src/bin/day3.rs Normal file
View file

@ -0,0 +1,110 @@
#![forbid(elided_lifetimes_in_paths, unsafe_code)]
use aoc23::read;
use chumsky::{prelude::*, text::int};
use std::collections::HashMap;
enum Entity {
Void,
Symbol(char),
Number { num: usize, len: usize }
}
impl Entity {
fn len(&self) -> usize {
match self {
Self::Void | Self::Symbol(_) => 1,
Self::Number { len, .. } => *len
}
}
fn parser() -> impl Parser<char, Self, Error = Simple<char>> {
choice((
just('.').map(|_| Self::Void),
int(10).map(|num: String| Self::Number {
num: num.parse().unwrap(),
len: num.len()
}),
none_of(['\n']).map(Self::Symbol)
))
}
}
struct Line {
entities: HashMap<usize, Entity>
}
impl Line {
fn parser() -> impl Parser<char, Self, Error = Simple<char>> {
Entity::parser().repeated().map(|line| {
let mut col = 0;
let mut entities = HashMap::new();
for ent in line {
let len = ent.len();
entities.insert(col, ent);
col += len;
}
Line { entities }
})
}
}
fn parser() -> impl Parser<char, Vec<Line>, Error = Simple<char>> {
Line::parser()
.then_ignore(just("\n"))
.repeated()
.then_ignore(end())
}
fn main() -> anyhow::Result<()> {
let grid = read("inputs/day3.txt", parser())?;
let mut sum = 0;
let mut possible_gears: HashMap<(usize, usize), Vec<usize>> = HashMap::new();
for (i, line) in grid.iter().enumerate() {
for (col, ent) in &line.entities {
let Entity::Number { num, len } = ent else {
continue;
};
let mut adjacent = false;
for search_row in i.saturating_sub(1) ..= i + 1 {
let Some(row) = grid.get(search_row) else {
continue;
};
for search_col in col.saturating_sub(1) ..= col + len {
if let Some(Entity::Symbol(symbol)) = row.entities.get(&search_col) {
adjacent = true;
if *symbol == '*' {
possible_gears
.entry((search_row, search_col))
.or_default()
.push(*num);
}
}
}
}
if adjacent {
sum += num;
}
}
}
println!("{sum}");
println!(
"{}",
possible_gears
.values()
.filter_map(|nums| {
let mut nums = nums.iter();
let ratio = nums.next()? * nums.next()?;
let None = nums.next() else {
return None;
};
Some(ratio)
})
.sum::<usize>()
);
Ok(())
}

View file

@ -70,3 +70,13 @@ where
anyhow!("Failed to parse input")
})
}
pub fn read_from_str<C, T>(from: &str, parser: C) -> anyhow::Result<T>
where
C: Parser<char, T, Error = Simple<char>>
{
parser.parse(from).map_err(|errors| {
report_err(from, "<input>", errors);
anyhow!("Failed to parse input")
})
}