Line data Source code
1 : #include "Grid2d.h"
2 : #include "LinewiseInput.h"
3 : #include "PuzzleImpl.h"
4 :
5 : #include <absl/container/flat_hash_set.h>
6 : #include <libassert/assert.hpp>
7 :
8 : #include <algorithm>
9 : #include <string_view>
10 :
11 : namespace {
12 : using Coord = Grid2d<char>::Coord;
13 : constexpr std::array<Coord, 4u> stencil{{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}};
14 :
15 : template <typename T> class PeriodicGridAdapter {
16 : public:
17 : using IndexType = Grid2dSpan<T>::IndexType;
18 : using Coord = Grid2dSpan<T>::Coord;
19 : PeriodicGridAdapter(Grid2dSpan<T> const &grid) : _grid(grid) {}
20 :
21 : T const &operator[](Coord const &c) const { return (*this)[c.x(), c.y()]; }
22 : T const &operator[](IndexType x, IndexType y) const {
23 : x %= _grid.xSize();
24 : y %= _grid.ySize();
25 : x = x < 0 ? x + _grid.xSize() : x;
26 : y = y < 0 ? y + _grid.ySize() : y;
27 : return _grid[x, y];
28 : }
29 :
30 : private:
31 : Grid2dSpan<T const> _grid;
32 : };
33 :
34 : } // namespace
35 :
36 1 : template <> size_t part1<2023, 21>(std::string_view const input) {
37 1 : constexpr size_t numSteps = 64;
38 :
39 1 : LinewiseInput lines(input);
40 1 : Grid2d<char> grid = lines.makeCharGrid2dWithGhostLayers(1, '#');
41 1 : Grid2d<char> tmpGrid = grid;
42 65 : for (size_t i = 0; i < numSteps; ++i) {
43 8448 : for (Coord c{0, 0}; c.y() < grid.ySize(); ++c.y())
44 1106688 : for (c.x() = 0; c.x() < grid.xSize(); ++c.x()) {
45 1098304 : if (grid[c] == '#')
46 133696 : continue;
47 :
48 964608 : tmpGrid[c] = '.';
49 :
50 3634169 : for (auto const &s : stencil) {
51 3634169 : Coord n = c + s;
52 3634169 : if (grid[n] == 'S') {
53 80465 : tmpGrid[c] = 'S';
54 80465 : break;
55 80465 : }
56 3634169 : }
57 964608 : }
58 64 : std::swap(grid, tmpGrid);
59 64 : }
60 1 : return grid.count('S');
61 1 : }
62 :
63 1 : template <> size_t part2<2023, 21>(std::string_view const) { return 0; }
|