Line data Source code
1 : #include "PuzzleImpl.h"
2 :
3 : #include "Grid2d.h"
4 : #include "LinewiseInput.h"
5 :
6 : #include <algorithm>
7 : #include <string_view>
8 : namespace {
9 :
10 : std::array<Grid2d<char>::Coord, 8u> constexpr stencil = {
11 : {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}}};
12 : }
13 :
14 1 : template <> std::string solvePart1<2025, 4>(std::string_view const input) {
15 1 : LinewiseInput linewise(input);
16 1 : Grid2d<char> grid = linewise.makeCharGrid2dWithGhostLayers(1, '.');
17 :
18 1 : uint64_t numAcessiblePaperRolls = 0;
19 141 : for (Grid2d<char>::Coord c{0, 0}; c.y() < grid.ySize(); ++c.y())
20 19740 : for (c.x() = 0; c.x() < grid.xSize(); ++c.x())
21 19600 : if (grid[c] == '@' &&
22 100824 : std::ranges::count_if(stencil, [&](auto const &d) { return grid[c + d] == '@'; }) < 4)
23 1626 : ++numAcessiblePaperRolls;
24 :
25 1 : return std::to_string(numAcessiblePaperRolls);
26 1 : }
27 :
28 1 : template <> std::string solvePart2<2025, 4>(std::string_view const input) {
29 1 : LinewiseInput linewise(input);
30 1 : Grid2d<char> grid = linewise.makeCharGrid2dWithGhostLayers(1, '.');
31 :
32 1 : uint64_t numPaperRollsRemoved = 0;
33 :
34 58 : for (bool haveRollsBeenRemoved = true; haveRollsBeenRemoved;) {
35 57 : haveRollsBeenRemoved = false;
36 8037 : for (Grid2d<char>::Coord c{0, 0}; c.y() < grid.ySize(); ++c.y())
37 1125180 : for (c.x() = 0; c.x() < grid.xSize(); ++c.x())
38 1117200 : if (grid[c] == '@' &&
39 1894632 : std::ranges::count_if(stencil, [&](auto const &d) { return grid[c + d] == '@'; }) < 4) {
40 9173 : grid[c] = '.';
41 9173 : ++numPaperRollsRemoved;
42 9173 : haveRollsBeenRemoved = true;
43 9173 : }
44 57 : }
45 :
46 1 : return std::to_string(numPaperRollsRemoved);
47 1 : }
|