Skip to content

Commit 45d6dd4

Browse files
committed
2025 day 4 part 2
1 parent 502e5d9 commit 45d6dd4

1 file changed

Lines changed: 30 additions & 8 deletions

File tree

src/y2025/day4.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::grid::{ALL_DIRS, Grid};
1+
use crate::grid::{ALL_DIRS, Grid, Pos};
22

33
pub struct Solver {
44
grid: Grid<Cell>,
@@ -12,16 +12,16 @@ impl crate::Puzzle for Solver {
1212
}
1313

1414
fn part1(&self) -> String {
15-
accessible_rolls(&self.grid).to_string()
15+
accessible_paper(&self.grid).len().to_string()
1616
}
1717

1818
fn part2(&self) -> String {
19-
"unimplemented".to_string()
19+
total_removeable_paper(&self.grid).to_string()
2020
}
2121
}
2222

23-
fn accessible_rolls(grid: &Grid<Cell>) -> u32 {
24-
let mut count = 0;
23+
fn accessible_paper(grid: &Grid<Cell>) -> Vec<Pos> {
24+
let mut positions = vec![];
2525
for (pos, cell) in grid.iter() {
2626
if matches!(cell, Cell::Paper)
2727
&& ALL_DIRS
@@ -30,10 +30,31 @@ fn accessible_rolls(grid: &Grid<Cell>) -> u32 {
3030
.count()
3131
< 4
3232
{
33-
count += 1;
33+
positions.push(pos);
3434
}
3535
}
36-
count
36+
positions
37+
}
38+
39+
// Removes accesible paper from the grid, returning the number of rolls that were removed
40+
fn remove_paper(grid: &mut Grid<Cell>) -> usize {
41+
let accessible = accessible_paper(grid);
42+
for p in &accessible {
43+
grid.set(*p, Cell::Empty);
44+
}
45+
accessible.len()
46+
}
47+
48+
fn total_removeable_paper(grid: &Grid<Cell>) -> usize {
49+
let mut grid = grid.clone();
50+
let mut total = 0;
51+
loop {
52+
let removed = remove_paper(&mut grid);
53+
if removed == 0 {
54+
return total;
55+
}
56+
total += removed;
57+
}
3758
}
3859

3960
fn parse_input(input: &str) -> Grid<Cell> {
@@ -71,5 +92,6 @@ fn test() {
7192
@.@.@@@.@.
7293
";
7394
let grid = parse_input(test_input);
74-
assert_eq!(13, accessible_rolls(&grid));
95+
assert_eq!(13, accessible_paper(&grid).len());
96+
assert_eq!(43, total_removeable_paper(&grid));
7597
}

0 commit comments

Comments
 (0)