Skip to content

Commit d735428

Browse files
committed
solve: day4
1 parent a6095fe commit d735428

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
resolver = "2"
33
members = [
44
"aoc_lib/aoc_derive",
5-
"aoc_lib/utils", "day1", "day2", "day3",
5+
"aoc_lib/utils", "day1", "day2", "day3", "day4",
66
]
77

88
[workspace.dependencies]

aoc_lib

day4/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "day4"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
aoc_derive.path = '../aoc_lib/aoc_derive'
8+
utils.path = '../aoc_lib/utils'
9+
derive_more.workspace = true
10+
itertools.workspace = true
11+
lazy-regex.workspace = true
12+
parse-display.workspace = true
13+
rayon.workspace = true
14+
regex.workspace = true
15+
num.workspace = true
16+
17+
[dev-dependencies]
18+
pretty_assertions.workspace = true

day4/src/main.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use aoc_derive::aoc_main;
2+
use derive_more::{Deref, DerefMut};
3+
use itertools::Itertools;
4+
use utils::{grid::Grid, math::Vec2D, *};
5+
6+
#[derive(Deref, DerefMut)]
7+
struct Map(Grid<char>);
8+
9+
impl Map {
10+
fn accessible_positions(&self) -> impl Iterator<Item = Vec2D> {
11+
self.iter().filter_map(|(pos, val)| {
12+
(val == &'@'
13+
&& self.all_neighbor_values(&pos).filter(|&neighbor| neighbor == &'@').count() < 4)
14+
.then_some(pos)
15+
})
16+
}
17+
18+
fn clear_paper(&mut self) {
19+
for pos in self.accessible_positions().collect_vec() {
20+
*self.get_mut(pos).unwrap() = '.'
21+
}
22+
}
23+
24+
fn clear_all(&mut self) -> usize {
25+
let mut count = 0;
26+
loop {
27+
let to_clear = self.accessible_positions().count();
28+
if to_clear == 0 {
29+
return count;
30+
}
31+
count += to_clear;
32+
self.clear_paper();
33+
}
34+
}
35+
}
36+
37+
#[aoc_main]
38+
fn solve(input: Input) -> impl Into<Solution> {
39+
let mut map = Map(input.char_grid());
40+
41+
(map.accessible_positions().count(), map.clear_all())
42+
}
43+
44+
#[cfg(test)]
45+
mod tests {
46+
use super::*;
47+
#[test]
48+
fn test_examples() {
49+
use utils::assert_example;
50+
assert_example!(
51+
r#"..@@.@@@@.
52+
@@@.@.@.@@
53+
@@@@@.@.@@
54+
@.@@@@..@.
55+
@@.@@@@.@@
56+
.@@@@@@@.@
57+
.@.@.@.@@@
58+
@.@@@.@@@@
59+
.@@@@@@@@.
60+
@.@.@@@.@."#,
61+
13,
62+
43
63+
);
64+
}
65+
}

0 commit comments

Comments
 (0)