AoC code coverage
Current view: top level - puzzles/2024 - Day04.cpp (source / functions) Coverage Total Hit
Test: master Lines: 100.0 % 34 34
Test Date: 2025-07-28 10:53:57 Functions: 100.0 % 3 3

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

Generated by: LCOV version 2.0-1