From 98d927cd72b5c5209c4a0516657598f3698f4f33 Mon Sep 17 00:00:00 2001 From: Joshua Smallwood <31932412+smallwoj@users.noreply.github.com> Date: Thu, 4 Dec 2025 07:54:45 -0600 Subject: [PATCH] feat: solve day 4 --- data/examples/04.txt | 11 ++++ src/bin/04.rs | 143 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 data/examples/04.txt create mode 100644 src/bin/04.rs diff --git a/data/examples/04.txt b/data/examples/04.txt new file mode 100644 index 0000000..ad7791d --- /dev/null +++ b/data/examples/04.txt @@ -0,0 +1,11 @@ +..@@.@@@@. +@@@.@.@.@@ +@@@@@.@.@@ +@.@@@@..@. +@@.@@@@.@@ +.@@@@@@@.@ +.@.@.@.@@@ +@.@@@.@@@@ +.@@@@@@@@. +@.@.@@@.@. + diff --git a/src/bin/04.rs b/src/bin/04.rs new file mode 100644 index 0000000..73cd87a --- /dev/null +++ b/src/bin/04.rs @@ -0,0 +1,143 @@ +advent_of_code::solution!(4); + +pub fn part_one(input: &str) -> Option { + let char_grid: Vec> = input + .split('\n') + .filter(|el| !el.is_empty()) + .map(|line| line.chars().collect()) + .collect(); + let mut num_rolls = 0; + for (i, row) in char_grid.iter().enumerate() { + for (j, c) in row.iter().enumerate() { + let mut num = 0; + if *c == '@' { + if (i + 1) < char_grid.len() && char_grid[i + 1][j] == '@' { + num += 1; + } + + if (i + 1) < char_grid.len() + && (j + 1) < row.len() + && char_grid[i + 1][j + 1] == '@' + { + num += 1; + } + + if (j + 1) < row.len() && char_grid[i][j + 1] == '@' { + num += 1; + } + + if (i as i32 - 1) >= 0 && (j + 1) < row.len() && char_grid[i - 1][j + 1] == '@' { + num += 1; + } + + if (i as i32 - 1) as i32 >= 0 && char_grid[i - 1][j] == '@' { + num += 1; + } + + if (i as i32 - 1) as i32 >= 0 + && (j as i32 - 1) >= 0 + && char_grid[i - 1][j - 1] == '@' + { + num += 1; + } + + if (j as i32 - 1) >= 0 && char_grid[i][j - 1] == '@' { + num += 1; + } + + if i + 1 < char_grid.len() && (j as i32 - 1) >= 0 && char_grid[i + 1][j - 1] == '@' + { + num += 1; + } + if num < 4 { + num_rolls += 1; + } + } + } + } + Some(num_rolls) +} + +pub fn part_two(input: &str) -> Option { + let mut char_grid: Vec> = input + .split('\n') + .filter(|el| !el.is_empty()) + .map(|line| line.chars().collect()) + .collect(); + let mut num_rolls = 0; + let mut done = false; + while !done { + done = true; + for (i, row) in char_grid.clone().iter().enumerate() { + for (j, c) in row.clone().iter().enumerate() { + let mut num = 0; + if *c == '@' { + if (i + 1) < char_grid.len() && char_grid[i + 1][j] == '@' { + num += 1; + } + + if (i + 1) < char_grid.len() + && (j + 1) < row.len() + && char_grid[i + 1][j + 1] == '@' + { + num += 1; + } + + if (j + 1) < row.len() && char_grid[i][j + 1] == '@' { + num += 1; + } + + if (i as i32 - 1) >= 0 && (j + 1) < row.len() && char_grid[i - 1][j + 1] == '@' + { + num += 1; + } + + if (i as i32 - 1) as i32 >= 0 && char_grid[i - 1][j] == '@' { + num += 1; + } + + if (i as i32 - 1) as i32 >= 0 + && (j as i32 - 1) >= 0 + && char_grid[i - 1][j - 1] == '@' + { + num += 1; + } + + if (j as i32 - 1) >= 0 && char_grid[i][j - 1] == '@' { + num += 1; + } + + if i + 1 < char_grid.len() + && (j as i32 - 1) >= 0 + && char_grid[i + 1][j - 1] == '@' + { + num += 1; + } + if num < 4 { + num_rolls += 1; + char_grid[i][j] = '.'; + done = false; + } + } + } + } + } + Some(num_rolls) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_part_one() { + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(13)); + } + + #[test] + fn test_part_two() { + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(43)); + } +}