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-07-28 10:53:57 Functions: 100.0 % 11 11

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

Generated by: LCOV version 2.0-1