Skip to content

Commit 55ca91c

Browse files
committed
2025 day 4: solve part 2
1 parent 732cbb8 commit 55ca91c

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Day 4:
22
13
3+
43

2025/src/day04.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@ int main(int argc, char **argv) {
1515

1616
auto grid = aoc::day04::read_input(infile);
1717

18-
auto part1 = aoc::day04::count_forklift_accessible(grid);
18+
int part1 = aoc::day04::count_forklift_accessible<aoc::PART_1>(grid);
1919
std::cout << part1 << "\n";
2020

21+
int part2 = 0;
22+
int removed;
23+
do {
24+
removed = aoc::day04::count_forklift_accessible<aoc::PART_2>(grid);
25+
part2 += removed;
26+
} while (removed > 0);
27+
std::cout << part2 << "\n";
28+
2129
return 0;
2230
}

2025/src/day04.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ namespace aoc::day04 {
1717

1818
using aoc::ds::Grid;
1919

20-
int count_forklift_accessible(const Grid<char> &grid) {
20+
template <aoc::Part PART>
21+
int count_forklift_accessible(Grid<char> &grid) {
2122
int count = 0;
22-
grid.for_each([&count, &grid](char c, const Pos &center) {
23+
grid.for_each([&count, &grid](char &c, const Pos &center) {
2324
if (c != '@') {
2425
return;
2526
}
@@ -32,6 +33,9 @@ int count_forklift_accessible(const Grid<char> &grid) {
3233
// the 4 in the question doesn't include the center roll
3334
if (adj_count < 5) {
3435
++count;
36+
if constexpr (PART == aoc::PART_2) {
37+
c = '.';
38+
}
3539
}
3640
});
3741
return count;

aoc_lib/src/ds/grid.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,26 @@ struct Grid {
339339
}
340340
}
341341

342+
/**
343+
* Calls func(value[, pos]) for each position in the grid. Allows in-place
344+
* modification of the value.
345+
*/
346+
template <typename Func>
347+
requires std::invocable<Func, value_type &, const Pos &> ||
348+
std::invocable<Func, value_type &>
349+
constexpr void for_each(Func &&func) {
350+
Pos p;
351+
for (p.y = 0; p.y < height; ++p.y) {
352+
for (p.x = 0; p.x < width; ++p.x) {
353+
if constexpr (std::invocable<Func, value_type &, const Pos &>) {
354+
func((*this)[p], p);
355+
} else {
356+
func((*this)[p]);
357+
}
358+
}
359+
}
360+
}
361+
342362
/**
343363
* Calls formatter(value[, pos]) for each position in the grid in row-major
344364
* order, and outputs a newline at the end of each row.

0 commit comments

Comments
 (0)