File tree Expand file tree Collapse file tree 4 files changed +36
-3
lines changed Expand file tree Collapse file tree 4 files changed +36
-3
lines changed Original file line number Diff line number Diff line change 11Day 4:
2213
3+ 43
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change @@ -17,9 +17,10 @@ namespace aoc::day04 {
1717
1818using 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 ¢er) {
23+ grid.for_each ([&count, &grid](char & c, const Pos ¢er) {
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;
Original file line number Diff line number Diff 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.
You can’t perform that action at this time.
0 commit comments