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