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

            Line data    Source code
       1              : #include "PuzzleImpl.h"
       2              : 
       3              : #include "Grid2d.h"
       4              : #include "Regex.h"
       5              : #include "Vector.h"
       6              : 
       7              : #include <chrono>
       8              : #include <format>
       9              : #include <print>
      10              : #include <string>
      11              : #include <thread>
      12              : 
      13              : namespace {
      14              : 
      15              : constexpr Vec2<int64_t> bathroomSize{101, 103};
      16              : 
      17              : struct Robot {
      18              :   Vec2<int64_t> p;
      19              :   Vec2<int64_t> v;
      20              : };
      21              : 
      22            1 : std::vector<Robot> parse(std::string_view const input) {
      23            1 :   std::vector<Robot> result;
      24            1 :   std::string regex = R"(p=(\d+),(\d+) v=(-?\d+),(-?\d+))";
      25            1 :   FindAndConsume<int64_t, int64_t, int64_t, int64_t> consume(regex);
      26          500 :   for (auto [px, py, vx, vy] : consume(input)) {
      27          500 :     result.emplace_back(Vec2<int64_t>{px, py}, Vec2<int64_t>{vx, vy});
      28          500 :   }
      29            1 :   return result;
      30            1 : }
      31              : 
      32              : } // namespace
      33              : 
      34            1 : template <> std::string solvePart1<2024, 14>(std::string_view const input) {
      35            1 :   std::vector<Robot> const robots = parse(input);
      36              : 
      37            1 :   constexpr Vec2<int64_t> center = bathroomSize / 2;
      38            1 :   std::array<unsigned, 4u> quadrantCount{};
      39              : 
      40            1 :   constexpr int seconds = 100;
      41              : 
      42          500 :   for (auto &r : robots) {
      43          500 :     Vec2<int64_t> p = ((r.p + seconds * r.v) % bathroomSize + bathroomSize) % bathroomSize;
      44          500 :     if (p.x() > center.x()) {
      45          235 :       if (p.y() > center.y()) {
      46          128 :         ++quadrantCount[0];
      47          128 :       } else if (p.y() < center.y()) {
      48          104 :         ++quadrantCount[1];
      49          104 :       }
      50          265 :     } else if (p.x() < center.x()) {
      51          259 :       if (p.y() > center.y()) {
      52          118 :         ++quadrantCount[2];
      53          141 :       } else if (p.y() < center.y()) {
      54          138 :         ++quadrantCount[3];
      55          138 :       }
      56          259 :     }
      57          500 :   }
      58              : 
      59            1 :   return std::to_string(std::ranges::fold_left(quadrantCount, int64_t(1), std::multiplies{}));
      60            1 : }
      61              : 
      62            1 : template <> std::string solvePart2<2024, 14>(std::string_view const) {
      63              : 
      64              :   // Code used for pattern evaluation with HI
      65              : 
      66              :   // std::vector<Robot> const robots = parse(input);
      67              : 
      68              :   // Grid2d<char> pic(bathroomSize, ' ');
      69              :   // auto const flippedPicView = pic.flipY();
      70              : 
      71              :   // constexpr int fps = 10;
      72              :   // constexpr std::chrono::duration<double> frameTime{1.0 / fps};
      73              : 
      74              :   // // for (int i = 90; i <= 6888; i += 103) { // horizontal pattern
      75              :   // // for (int i = 20; i <= 6888; i += 101) { // vertical pattern
      76              :   // for (int i = 0; i <= 6888; ++i) { // all frames
      77              :   //   auto const frameStart = std::chrono::steady_clock::now();
      78              :   //   pic.fill(' ');
      79              :   //   for (auto &r : robots) {
      80              :   //     Vec2<int64_t> p = ((r.p + i * r.v) % bathroomSize + bathroomSize) % bathroomSize;
      81              :   //     pic[p] = 'X';
      82              :   //   }
      83              :   //   std::println("Time: {}s:\n{}\n\n\n", i, flippedPicView);
      84              :   //   std::this_thread::sleep_until(frameStart + frameTime);
      85              :   // }
      86              : 
      87            1 :   return "6888";
      88            1 : }
        

Generated by: LCOV version 2.0-1