diff options
author | Guillermo Ramos | 2024-12-10 19:56:19 +0100 |
---|---|---|
committer | Guillermo Ramos | 2024-12-11 09:56:58 +0100 |
commit | a360df726d04bf49fc8a06fdcbf668f1fea98aff (patch) | |
tree | daf394808d08f61d9af227eddf8bec0db036cee3 | |
parent | 160caef50246ac9a62f596522598b1dca3fabb29 (diff) | |
download | AoC-a360df726d04bf49fc8a06fdcbf668f1fea98aff.tar.gz |
2024.10
-rw-r--r-- | 2024_rust/inputs/10 | 52 | ||||
-rw-r--r-- | 2024_rust/src/bin/day10.rs | 68 | ||||
-rw-r--r-- | 2024_rust/src/lib.rs | 13 |
3 files changed, 133 insertions, 0 deletions
diff --git a/2024_rust/inputs/10 b/2024_rust/inputs/10 new file mode 100644 index 0000000..e775373 --- /dev/null +++ b/2024_rust/inputs/10 @@ -0,0 +1,52 @@ +5654348767654328943210568976534369910789674323105678 +8701239458901217658121477687623478823658789410234089 +9687012344567806789054389598510562734569876501870123 +7896543569478910690343201423421051650121965432965434 +3234512478321001541252102310436710543230892396566985 +0105601309010192432567010121045827652340181087654876 +3238711210520983401478121012310938981653276321049965 +4589100345601276512389652983429845670787345438038734 +7671018986798345601456743876536762103894421589125621 +8562327019887016512567895805445653214545430677894100 +9443456523456325434898656914560344367656528912763210 +2356932108701236721019847823271255698767817603454323 +1047823019854349832478739854180766789678906541065410 +2154014323769858210567026765099850176543215432878929 +3763005458958765523450110623458901289012103898945678 +7892176767567894654543210210567110378650112367630567 +4567989853018723743210321671003025496743203456721656 +3298789914329610894987434582312136789867654109876545 +0107678801458543043406535495443249834578943218103430 +1014565432367232112312345456954956725669867823212321 +0123054545850187601432196307867875218754756984654321 +1232123456987696566545087212987890109843405498789210 +8741876567896501487656996501256765210762112301230101 +9650985401223432398747887450345034369854013010149877 +8967812370314671054321076365432123456343401323456778 +7874901987404589969965431256501432369265692454987569 +2965543256563217878872120387124501078104783467873456 +1234630159870101767013098591037612156943476550012347 +0323701068965432656523107652348765747872105621891298 +3410892454321140545432112543659877831030323436760101 +6598543265210061236789093456701436921121410345603456 +5677610178901176678768186569892345430236587107812367 +0189825679543285789451076654385454321947496236901098 +1670134389056994874342345789276542100858905145610329 +2765245212167823965218763210167033103767814098783410 +3874396103456710454109454109098124912980123045696598 +4983087023469832310087345678567865843832652136787867 +5672171110578761101296218778410976756741743421691958 +9876780987645670814385609569328985401650898330540349 +6565491234532189985674212432310670332798567891231210 +5430303456541023476543203421211591243897678780010676 +6321212187650116567800134560101487654016549654321985 +7890341098565207656912129870122371210323434545401014 +6088780879876348745923038565430110326546783256910123 +2109691965665489632874567678321065487235490127823434 +1234532234786543211065212589832966599104381030198569 +0398940123894678707845103492123877678567210943217678 +1787654014783289698934598783034568943498219854006010 +5671013005654194567827647695693210012382108760125421 +0542562196678003430918034506784345894563456978934438 +1233478787549112321209123215410256723874567834983549 +2012989101238103012213230123322105012965450125676678 diff --git a/2024_rust/src/bin/day10.rs b/2024_rust/src/bin/day10.rs new file mode 100644 index 0000000..3c208bd --- /dev/null +++ b/2024_rust/src/bin/day10.rs @@ -0,0 +1,68 @@ +use aoc2024::matrix; +use std::collections::HashSet; + +static AROUND_DELTAS: [(isize, isize); 4] = [(-1, 0), (0, -1), (0, 1), (1, 0)]; + +type Matrix = matrix::Matrix<u32>; +type Trailhead = matrix::Pos; + +fn trailheads(m: &Matrix) -> Vec<Trailhead> { + let mut result: Vec<Trailhead> = vec![]; + for i in 0..m.limit.0 { + for j in 0..m.limit.1 { + let pos = (i, j); + if m.get(pos) == &0 { + result.push(pos); + } + } + } + result +} + +fn reachable(m: &Matrix, pos: matrix::Pos) -> HashSet<matrix::Pos> { + let height = m.get(pos); + AROUND_DELTAS + .iter() + .filter_map(|&d| m.pos_move(pos, d)) + .filter(|&p| *m.get(p) == height + 1) + .collect() +} + +fn score1(m: &Matrix, th: Trailhead) -> u32 { + let mut heads: HashSet<matrix::Pos> = HashSet::new(); + heads.insert(th); + let mut height = 0; + while height < 9 { + heads = heads.into_iter().flat_map(|th| reachable(m, th)).collect(); + height += 1; + } + heads.len() as u32 +} + +fn p1(input: &str) -> String { + let m: Matrix = matrix::Matrix::new(input, |c| c.to_digit(10).unwrap()); + let result: u32 = trailheads(&m).iter().map(|&th| score1(&m, th)).sum(); + + result.to_string() +} + +fn score2(m: &Matrix, th: Trailhead) -> u32 { + let mut heads: Vec<matrix::Pos> = vec![th]; + let mut height = 0; + while height < 9 { + heads = heads.into_iter().flat_map(|th| reachable(m, th)).collect(); + height += 1; + } + heads.len() as u32 +} + +fn p2(input: &str) -> String { + let m: Matrix = matrix::Matrix::new(input, |c| c.to_digit(10).unwrap()); + let result: u32 = trailheads(&m).iter().map(|&th| score2(&m, th)).sum(); + + result.to_string() +} + +fn main() { + aoc2024::run_day("10", Some(p1), Some(p2)); +} diff --git a/2024_rust/src/lib.rs b/2024_rust/src/lib.rs index c034fea..ea6f7d6 100644 --- a/2024_rust/src/lib.rs +++ b/2024_rust/src/lib.rs @@ -1,5 +1,6 @@ pub mod matrix { pub type Pos = (usize, usize); + pub type PosDelta = (isize, isize); #[derive(Clone)] pub struct Matrix<T> { @@ -27,6 +28,18 @@ pub mod matrix { pub fn set(&mut self, (x, y): Pos, dot: T) { self.dots[x][y] = dot; } + + pub fn pos_move(&self, (x, y): Pos, (dx, dy): PosDelta) -> Option<Pos> { + let x2 = x as isize + dx; + if x2 < 0 || x2 >= self.limit.0 as isize { + return None; + } + let y2 = y as isize + dy; + if y2 < 0 || y2 >= self.limit.1 as isize { + return None; + } + Some((x2 as usize, y2 as usize)) + } } use std::fmt; |