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

            Line data    Source code
       1              : #include "PuzzleImpl.h"
       2              : 
       3              : #include "Regex.h"
       4              : #include "Vector.h"
       5              : 
       6              : #include <numeric>
       7              : #include <string>
       8              : 
       9              : namespace {
      10              : 
      11              : struct ClawMachine {
      12              :   Vec2<int64_t> a;
      13              :   Vec2<int64_t> b;
      14              :   Vec2<int64_t> p;
      15              : };
      16              : 
      17            2 : std::vector<ClawMachine> parse(std::string_view const input, Vec2<int64_t> add = {0, 0}) {
      18            2 :   std::vector<ClawMachine> result;
      19            2 :   std::string regex =
      20            2 :       R"(Button A\: X\+(\d+), Y\+(\d+)\nButton B\: X\+(\d+), Y\+(\d+)\nPrize\: X=(\d+), Y=(\d+))";
      21            2 :   FindAndConsume<int64_t, int64_t, int64_t, int64_t, int64_t, int64_t> consume(regex);
      22          640 :   for (auto [ax, ay, bx, by, px, py] : consume(input)) {
      23          640 :     result.emplace_back(Vec2<int64_t>{ax, ay}, Vec2<int64_t>{bx, by}, Vec2<int64_t>{px, py} + add);
      24          640 :   }
      25            2 :   return result;
      26            2 : }
      27              : 
      28          640 : size_t solve(ClawMachine const &machine) {
      29          640 :   Vec2<double> const a{machine.a};
      30          640 :   Vec2<double> const b{machine.b};
      31          640 :   Vec2<double> const p{machine.p};
      32              : 
      33          640 :   Vec2<double> const n =
      34          640 :       Vec2<double>{b.y() * p.x() - b.x() * p.y(), a.x() * p.y() - a.y() * p.x()} /
      35          640 :       (a.x() * b.y() - a.y() * b.x());
      36              : 
      37          640 :   Vec2<int64_t> in{static_cast<int64_t>(std::round(n.x())),
      38          640 :                    static_cast<int64_t>(std::round(n.y()))};
      39              : 
      40          640 :   if (in.x() * machine.a + in.y() * machine.b == machine.p)
      41          320 :     return in.x() * 3 + in.y();
      42          320 :   else
      43          320 :     return 0;
      44          640 : }
      45              : 
      46              : } // namespace
      47              : 
      48            1 : template <> std::string solvePart1<2024, 13>(std::string_view const input) {
      49            1 :   auto machines = parse(input);
      50            1 :   return std::to_string(
      51            1 :       std::transform_reduce(machines.begin(), machines.end(), size_t(0), std::plus{}, solve));
      52            1 : }
      53              : 
      54            1 : template <> std::string solvePart2<2024, 13>(std::string_view const input) {
      55            1 :   auto machines = parse(input, Vec2<int64_t>(10000000000000, 10000000000000));
      56            1 :   return std::to_string(
      57            1 :       std::transform_reduce(machines.begin(), machines.end(), size_t(0), std::plus{}, solve));
      58            1 : }
        

Generated by: LCOV version 2.0-1