Skip to content

Commit 622eb3e

Browse files
committed
Year 2025 Day 4
1 parent 5b199c0 commit 622eb3e

File tree

7 files changed

+85
-4
lines changed

7 files changed

+85
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
8080
| 1 | [Secret Entrance](https://adventofcode.com/2025/day/1) | [Source](src/year2025/day01.rs) | 25 |
8181
| 2 | [Gift Shop](https://adventofcode.com/2025/day/2) | [Source](src/year2025/day02.rs) | 1 |
8282
| 3 | [Lobby](https://adventofcode.com/2025/day/3) | [Source](src/year2025/day03.rs) | 18 |
83+
| 4 | [Printing Department](https://adventofcode.com/2025/day/4) | [Source](src/year2025/day04.rs) | 244 |
8384

8485
## 2024
8586

benches/benchmark.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,5 @@ benchmark!(year2024
9595
);
9696

9797
benchmark!(year2025
98-
day01, day02, day03
98+
day01, day02, day03, day04
9999
);

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,5 @@ library!(year2024 "Locate the Chief Historian in time for the big Christmas slei
7474
);
7575

7676
library!(year2025 "Finish the North Pole decorations in time for Christmas."
77-
day01, day02, day03
77+
day01, day02, day03, day04
7878
);

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,5 @@ run!(year2024
141141
);
142142

143143
run!(year2025
144-
day01, day02, day03
144+
day01, day02, day03, day04
145145
);

src/year2025/day04.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//! # Printing Department
2+
use crate::util::grid::*;
3+
use crate::util::point::*;
4+
5+
type Input = (usize, usize);
6+
7+
pub fn parse(input: &str) -> Input {
8+
let grid = Grid::parse(input);
9+
let offset = Point::new(1, 1);
10+
let mut todo = Vec::new();
11+
let mut padded = Grid::new(grid.width + 2, grid.height + 2, u8::MAX);
12+
13+
for y in 0..grid.height {
14+
for x in 0..grid.width {
15+
let point = Point::new(x, y);
16+
17+
if grid[point] == b'@' {
18+
let count = DIAGONAL
19+
.iter()
20+
.map(|&d| point + d)
21+
.filter(|&next| grid.contains(next) && grid[next] == b'@')
22+
.count();
23+
24+
if count < 4 {
25+
todo.push(point + offset);
26+
}
27+
padded[point + offset] = count as u8;
28+
}
29+
}
30+
}
31+
32+
let part_one = todo.len();
33+
let mut part_two = 0;
34+
35+
while let Some(point) = todo.pop() {
36+
part_two += 1;
37+
38+
for next in DIAGONAL.map(|d| point + d) {
39+
if padded[next] == 4 {
40+
todo.push(next);
41+
}
42+
padded[next] -= 1;
43+
}
44+
}
45+
46+
(part_one, part_two)
47+
}
48+
49+
pub fn part1(input: &Input) -> usize {
50+
input.0
51+
}
52+
53+
pub fn part2(input: &Input) -> usize {
54+
input.1
55+
}

tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,5 @@ test!(year2024
8787
);
8888

8989
test!(year2025
90-
day01, day02, day03
90+
day01, day02, day03, day04
9191
);

tests/year2025/day04.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use aoc::year2025::day04::*;
2+
3+
const EXAMPLE: &str = "\
4+
..@@.@@@@.
5+
@@@.@.@.@@
6+
@@@@@.@.@@
7+
@.@@@@..@.
8+
@@.@@@@.@@
9+
.@@@@@@@.@
10+
.@.@.@.@@@
11+
@.@@@.@@@@
12+
.@@@@@@@@.
13+
@.@.@@@.@.";
14+
15+
#[test]
16+
fn part1_test() {
17+
let input = parse(EXAMPLE);
18+
assert_eq!(part1(&input), 13);
19+
}
20+
21+
#[test]
22+
fn part2_test() {
23+
let input = parse(EXAMPLE);
24+
assert_eq!(part2(&input), 43);
25+
}

0 commit comments

Comments
 (0)