Skip to content

Commit 86591a1

Browse files
committed
[2025] Day 4 first solution
1 parent 2255de5 commit 86591a1

File tree

4 files changed

+124
-10
lines changed

4 files changed

+124
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Thank you to [Eric Wastl](http://was.tl) for running this incredible yearly even
88
- [Day 01: Secret Entrance](aoc_2025/src/day_01.rs)
99
- [Day 02: Gift Shop](aoc_2025/src/day_02.rs)
1010
- [Day 03: Lobby](aoc_2025/src/day_03.rs)
11+
- [Day 04: Printing Department](aoc_2025/src/day_04.rs)
1112
<!-- MARKER -->
1213

1314
## [2024](https://adventofcode.com/2024) [![aoc_2024](https://github.com/connorslade/advent-of-code/actions/workflows/aoc_2024.yml/badge.svg)](https://github.com/connorslade/advent-of-code/actions/workflows/aoc_2024.yml)

aoc_2025/src/day_03.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,21 @@ fn solve(input: &str, n: usize) -> u64 {
2424
}
2525

2626
fn joltage(digits: &[u8], n: usize) -> u64 {
27-
let mut num = 0;
28-
let mut last_used = 0;
29-
30-
for bat in 0..n {
31-
let size = digits.len() - (n - 1 - bat) - last_used;
32-
let slice = &digits[last_used..(last_used + size).min(digits.len())];
27+
let mut out = 0;
28+
let mut last = 0;
3329

30+
for bat in 1..=n {
31+
let size = digits.len() - (n - bat) - last;
3432
for i in (0..=9).rev() {
35-
if let Some(idx) = slice.iter().position(|x| *x == i) {
36-
last_used += idx + 1;
37-
num = num * 10 + i as u64;
33+
if let Some(idx) = digits.iter().skip(last).take(size).position(|x| *x == i) {
34+
last += idx + 1;
35+
out = out * 10 + i as u64;
3836
break;
3937
}
4038
}
4139
}
4240

43-
num
41+
out
4442
}
4543

4644
#[cfg(test)]

aoc_2025/src/day_04.rs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
use aoc_lib::matrix::Grid;
2+
use common::{Answer, solution};
3+
use nd_vec::Vec2;
4+
5+
solution!("Printing Department", 4);
6+
7+
#[derive(Clone)]
8+
enum Tile {
9+
Empty,
10+
Roll,
11+
}
12+
13+
fn part_a(input: &str) -> Answer {
14+
let grid = Grid::parse(input, |chr| match chr {
15+
'.' => Tile::Empty,
16+
'@' => Tile::Roll,
17+
_ => panic!(),
18+
});
19+
20+
count(&grid).0.into()
21+
}
22+
23+
fn part_b(input: &str) -> Answer {
24+
let mut grid = Grid::parse(input, |chr| match chr {
25+
'.' => Tile::Empty,
26+
'@' => Tile::Roll,
27+
_ => panic!(),
28+
});
29+
30+
let mut out = 0;
31+
32+
loop {
33+
let (removed, next) = count(&grid);
34+
out += removed;
35+
grid = next;
36+
if removed == 0 {
37+
break;
38+
}
39+
}
40+
41+
out.into()
42+
}
43+
44+
fn count(grid: &Grid<Tile>) -> (u32, Grid<Tile>) {
45+
let mut out = 0;
46+
47+
let mut next = grid.clone();
48+
49+
for y in 0..grid.size.y() {
50+
for x in 0..grid.size().x() {
51+
let pos = Vec2::new([x, y]).num_cast::<i32>().unwrap();
52+
if matches!(grid.get(pos.num_cast().unwrap()).unwrap(), Tile::Empty) {
53+
continue;
54+
}
55+
56+
let mut neighbors = 0;
57+
for dy in -1..=1 {
58+
for dx in -1..=1 {
59+
let delta = Vec2::new([dx, dy]);
60+
let check = pos + delta;
61+
62+
if delta == Vec2::zero()
63+
|| check.x() < 0
64+
|| check.y() < 0
65+
|| check.x() >= grid.size.x() as i32
66+
|| check.y() >= grid.size.y() as i32
67+
{
68+
continue;
69+
}
70+
71+
if matches!(grid.get(check.num_cast().unwrap()).unwrap(), Tile::Roll) {
72+
neighbors += 1;
73+
}
74+
}
75+
}
76+
77+
if neighbors < 4 {
78+
out += 1;
79+
next.set(pos, Tile::Empty);
80+
}
81+
}
82+
}
83+
84+
(out, next)
85+
}
86+
87+
#[cfg(test)]
88+
mod test {
89+
use indoc::indoc;
90+
91+
const CASE: &str = indoc! {"
92+
..@@.@@@@.
93+
@@@.@.@.@@
94+
@@@@@.@.@@
95+
@.@@@@..@.
96+
@@.@@@@.@@
97+
.@@@@@@@.@
98+
.@.@.@.@@@
99+
@.@@@.@@@@
100+
.@@@@@@@@.
101+
@.@.@@@.@.
102+
"};
103+
104+
#[test]
105+
fn part_a() {
106+
assert_eq!(super::part_a(CASE), 13.into());
107+
}
108+
109+
#[test]
110+
fn part_b() {
111+
assert_eq!(super::part_b(CASE), 43.into());
112+
}
113+
}

aoc_2025/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ use common::Solution;
33
mod day_01;
44
mod day_02;
55
mod day_03;
6+
mod day_04;
67
// [import_marker]
78

89
pub const SOLUTIONS: &[Solution] = &[
910
day_01::SOLUTION,
1011
day_02::SOLUTION,
1112
day_03::SOLUTION,
13+
day_04::SOLUTION,
1214
// [list_marker]
1315
];

0 commit comments

Comments
 (0)