AoC code coverage
Current view: top level - puzzles/2015 - Day06.cpp (source / functions) Coverage Total Hit
Test: master Lines: 100.0 % 55 55
Test Date: 2025-12-11 19:43:23 Functions: 100.0 % 11 11

            Line data    Source code
       1              : #include "Grid2d.h"
       2              : #include "LinewiseInput.h"
       3              : #include "PuzzleImpl.h"
       4              : 
       5              : #include <re2/re2.h>
       6              : 
       7              : #include <algorithm>
       8              : #include <string_view>
       9              : 
      10              : namespace {
      11              : struct Operation {
      12          600 :   Operation(std::string_view const s) {
      13          600 :     static RE2 const pattern(R"((turn on|turn off|toggle) (\d+),(\d+) through (\d+),(\d+))");
      14          600 :     std::string_view operation;
      15          600 :     RE2::FullMatch(s, pattern, &operation, &begin.x(), &begin.y(), &end.x(), &end.y());
      16          600 :     if (operation == "turn on") {
      17          202 :       op = ON;
      18          398 :     } else if (operation == "turn off") {
      19          204 :       op = OFF;
      20          204 :     } else {
      21          194 :       op = TOGGLE;
      22          194 :     }
      23              : 
      24          600 :     ++end.x();
      25          600 :     ++end.y();
      26          600 :   }
      27              : 
      28          300 :   void applyPt1(Grid2d<char> &grid) {
      29          300 :     switch (op) {
      30          101 :     case ON:
      31      8825195 :       grid.subgrid(begin, end).forEach([](char &e) { e = 1; });
      32          101 :       break;
      33          102 :     case OFF:
      34      6258300 :       grid.subgrid(begin, end).forEach([](char &e) { e = 0; });
      35          102 :       break;
      36           97 :     case TOGGLE:
      37      5414706 :       grid.subgrid(begin, end).forEach([](char &e) { e = e == 0 ? 1 : 0; });
      38          300 :     }
      39          300 :   }
      40              : 
      41          300 :   void applyPt2(Grid2d<int> &grid) {
      42          300 :     switch (op) {
      43          101 :     case ON:
      44      8825195 :       grid.subgrid(begin, end).forEach([](int &e) { e += 1; });
      45          101 :       break;
      46          102 :     case OFF:
      47      6258300 :       grid.subgrid(begin, end).forEach([](int &e) { e = std::max(e - 1, 0); });
      48          102 :       break;
      49           97 :     case TOGGLE:
      50      5414706 :       grid.subgrid(begin, end).forEach([](int &e) { e += 2; });
      51          300 :     }
      52          300 :   }
      53              : 
      54              :   enum Op : std::uint8_t { ON, OFF, TOGGLE };
      55              : 
      56              :   Grid2d<char>::Coord begin;
      57              :   Grid2d<char>::Coord end;
      58              :   Op op;
      59              : };
      60              : 
      61              : } // namespace
      62              : 
      63            1 : template <> std::string solvePart1<2015, 6>(std::string_view const input) {
      64            1 :   LinewiseInput lines(input);
      65            1 :   Grid2d<char> grid(1000, 1000, 0);
      66            1 :   static RE2 const pattern(R"((turn on|turn off|toggle) (\d+),(\d+) through (\d+),(\d+))");
      67          300 :   for (auto const &l : getLineWise(input)) {
      68          300 :     Operation(l).applyPt1(grid);
      69          300 :   }
      70            1 :   return std::to_string(grid.count(1));
      71            1 : }
      72              : 
      73            1 : template <> std::string solvePart2<2015, 6>(std::string_view const input) {
      74            1 :   Grid2d<int> grid(1000, 1000, 0);
      75            1 :   static RE2 const pattern(R"((turn on|turn off|toggle) (\d+),(\d+) through (\d+),(\d+))");
      76          300 :   for (auto const &l : getLineWise(input)) {
      77          300 :     Operation(l).applyPt2(grid);
      78          300 :   }
      79            1 :   return std::to_string(grid.sum());
      80            1 : }
        

Generated by: LCOV version 2.0-1