From 161ba78efe779454aedf8321163df30c0a4d2005 Mon Sep 17 00:00:00 2001 From: Timo Schneider Date: Tue, 9 Dec 2025 21:13:37 +0100 Subject: [PATCH] finished day04 --- README.md | 22 +++++++-------- src/day04/common.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++ src/day04/mod.rs | 2 +- src/day04/part04a.rs | 47 ++------------------------------ src/day04/part04b.rs | 10 ++++++- 5 files changed, 87 insertions(+), 58 deletions(-) create mode 100644 src/day04/common.rs diff --git a/README.md b/README.md index 9ad2b5d..eba34ac 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,23 @@ # Advent of Code 2025 ```bash -day01:A => 1150 ( 52us) -day01:B => 1150 ( 50us) -day02:A => 31839939622 ( 9337us) -day02:B => 41662374059 ( 68756us) +day01:A => 1150 ( 50us) +day01:B => 1150 ( 48us) +day02:A => 31839939622 ( 7730us) +day02:B => 41662374059 ( 64909us) day03:A => 16812 ( 83us) -day03:B => 166345822896410 ( 292us) -day04:A => 1518 ( 190us) -day04:B => 0 ( 0us) +day03:B => 166345822896410 ( 285us) +day04:A => 1518 ( 194us) +day04:B => 8665 ( 3753us) day05:A => 0 ( 0us) day05:B => 0 ( 0us) -day06:A => 6503327062445 ( 201us) -day06:B => 9640641878593 ( 135us) +day06:A => 6503327062445 ( 196us) +day06:B => 9640641878593 ( 134us) day07:A => 0 ( 0us) day07:B => 0 ( 0us) day08:A => 0 ( 0us) day08:B => 0 ( 0us) -day09:A => 4725826296 ( 160us) +day09:A => 4725826296 ( 165us) day09:B => 0 ( 0us) ---------- - 81842us + 78131us ``` \ No newline at end of file diff --git a/src/day04/common.rs b/src/day04/common.rs new file mode 100644 index 0000000..306982f --- /dev/null +++ b/src/day04/common.rs @@ -0,0 +1,64 @@ + +pub fn parse_to_floor(input: String) -> Vec> { + let mut rows: Vec> = Vec::with_capacity(input.len() + 2); + rows.push(vec![0u8; input.len()]); + rows.append( + &mut input + .lines() + .map(|line| { + let mut col: Vec = Vec::with_capacity(line.len() + 2); + col.push(0u8); + col.append( + &mut line + .chars() + .map(|c| match c { + '@' => 1u8, + _ => 0u8, + }) + .collect(), + ); + col.push(0u8); + col + }) + .collect(), + ); + rows.push(vec![0u8; input.len()]); + + rows +} + +pub fn remove_if_possible(floor: &mut Vec>) -> Option { + let mut removed: Vec<(usize, usize)> = Vec::new(); + + floor + .windows(3) + .enumerate() + .for_each(|(r, rows)| { + rows[0] + .iter() + .zip(rows[1].iter()) + .zip(rows[2].iter()) + .map(|((a, b), c)| (a, b, c)) + .collect::>() + .windows(3) + .enumerate() + .for_each(|(c, cols)| { + if cols[1].1 == &0u8 { + return; + } + if cols.iter().map(|(a, b, c)| *a + *b + *c).sum::() - 1 < 4 { + removed.push((r+1, c+1)); + } + }); + }); + + removed.iter().for_each(|(r, c)| { + floor[*r][*c] = 0; + }); + + if removed.len() == 0 { + return None; + } + + Some(removed.len() as u64) +} \ No newline at end of file diff --git a/src/day04/mod.rs b/src/day04/mod.rs index 9978634..69252e6 100644 --- a/src/day04/mod.rs +++ b/src/day04/mod.rs @@ -1,5 +1,6 @@ pub mod part04a; pub mod part04b; +mod common; #[cfg(test)] mod tests { @@ -22,7 +23,6 @@ mod tests { } #[test] - #[ignore] fn part04b() { assert_eq!(part04b::solve(PUZZLE.to_string()), 43); } diff --git a/src/day04/part04a.rs b/src/day04/part04a.rs index a7130b0..4dc585f 100644 --- a/src/day04/part04a.rs +++ b/src/day04/part04a.rs @@ -1,48 +1,5 @@ +use crate::day04::common::{parse_to_floor, remove_if_possible}; pub fn solve(puzzle: String) -> u64 { - let mut solution: u64 = 0; - - let mut rows: Vec> = Vec::with_capacity(puzzle.len() + 2); - rows.push(vec![0u8; puzzle.len()]); - rows.append( - &mut puzzle - .lines() - .map(|line| { - let mut col: Vec = Vec::with_capacity(line.len() + 2); - col.push(0u8); - col.append( - &mut line - .chars() - .map(|c| match c { - '@' => 1u8, - _ => 0u8, - }) - .collect(), - ); - col.push(0u8); - col - }) - .collect(), - ); - rows.push(vec![0u8; puzzle.len()]); - - rows.windows(3).for_each(|rows| { - rows[0] - .iter() - .zip(rows[1].iter()) - .zip(rows[2].iter()) - .map(|((a, b), c)| (*a, *b, *c)) - .collect::>() - .windows(3) - .for_each(|cols| { - if cols[1].1 != 1u8 { - return; - } - if cols.iter().map(|(a, b, c)| *a + *b + *c).sum::() - 1 < 4 { - solution += 1; - } - }); - }); - - solution + remove_if_possible(&mut parse_to_floor(puzzle)).unwrap_or_else(|| 0) } diff --git a/src/day04/part04b.rs b/src/day04/part04b.rs index 10fe1d8..0bc78cf 100644 --- a/src/day04/part04b.rs +++ b/src/day04/part04b.rs @@ -1,6 +1,14 @@ +use crate::day04::common::{parse_to_floor, remove_if_possible}; pub fn solve(puzzle: String) -> u64 { - 0 // TODO + let mut solution: u64 = 0; + let mut floor = parse_to_floor(puzzle); + + while let Some(removed) = remove_if_possible(&mut floor) { + solution += removed; + } + + solution }