Line data Source code
1 : #include "Grid2d.h"
2 : #include "LinewiseInput.h"
3 : #include "PuzzleImpl.h"
4 :
5 : #include <algorithm>
6 : #include <string>
7 :
8 1 : template <> std::string solvePart1<2024, 4>(std::string_view const input) {
9 1 : LinewiseInput linewise(input);
10 1 : Grid2d<char> grid = linewise.makeCharGrid2dWithGhostLayers(3, '.');
11 :
12 1 : std::array<Grid2d<char>::Coord, 8u> constexpr dirs = {
13 1 : {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}}};
14 :
15 1 : std::string_view constexpr word = "XMAS";
16 :
17 1 : uint64_t count = 0;
18 141 : for (Grid2d<char>::Coord c{0, 0}; c.y() < grid.ySize(); ++c.y())
19 19740 : for (c.x() = 0; c.x() < grid.xSize(); ++c.x())
20 19600 : if (grid[c] == 'X') {
21 30096 : count += std::ranges::count_if(dirs, [&](auto const &d) {
22 49814 : for (int i = 1; i < 4; ++i)
23 47267 : if (grid[c + i * d] != word[i])
24 27549 : return false;
25 2547 : return true;
26 30096 : });
27 3762 : }
28 :
29 1 : return std::to_string(count);
30 1 : }
31 :
32 1 : template <> std::string solvePart2<2024, 4>(std::string_view const input) {
33 1 : LinewiseInput linewise(input);
34 1 : Grid2d<char> grid = linewise.makeCharGrid2d();
35 :
36 1 : uint64_t cnt = 0;
37 139 : for (int y = 1; y < grid.ySize() - 1; ++y)
38 19182 : for (int x = 1; x < grid.xSize() - 1; ++x)
39 19044 : if (grid[x, y] == 'A' &&
40 19044 : ((grid[x - 1, y - 1] == 'M' && grid[x + 1, y + 1] == 'S') ||
41 4865 : (grid[x - 1, y - 1] == 'S' && grid[x + 1, y + 1] == 'M')) &&
42 19044 : ((grid[x - 1, y + 1] == 'M' && grid[x + 1, y - 1] == 'S') ||
43 2751 : (grid[x - 1, y + 1] == 'S' && grid[x + 1, y - 1] == 'M'))) {
44 1939 : ++cnt;
45 1939 : }
46 :
47 1 : return std::to_string(cnt);
48 1 : }
|