AoC code coverage
Current view: top level - puzzles/2025 - Day03.cpp (source / functions) Coverage Total Hit
Test: master Lines: 71.4 % 28 20
Test Date: 2025-12-11 19:43:23 Functions: 80.0 % 5 4

            Line data    Source code
       1              : #include "PuzzleImpl.h"
       2              : 
       3              : #include "Views.h"
       4              : 
       5              : #include <algorithm>
       6              : #include <array>
       7              : #include <cstdint>
       8              : #include <limits>
       9              : #include <ranges>
      10              : #include <string>
      11              : #include <string_view>
      12              : 
      13              : namespace {
      14              : 
      15              : constexpr auto numDigits = std::numeric_limits<uint64_t>::digits10;
      16              : 
      17            0 : constexpr std::array<uint64_t, numDigits> computePowersOf10() {
      18            0 :   std::array<uint64_t, numDigits> result{};
      19            0 :   result[0] = 1;
      20            0 :   for (size_t i = 1; i < result.size(); ++i) {
      21            0 :     result[i] = result[i - 1] * 10;
      22            0 :   }
      23            0 :   return result;
      24            0 : }
      25              : 
      26              : constexpr std::array<uint64_t, numDigits> powersOf10 = computePowersOf10();
      27              : 
      28          400 : template <int numBatteries> uint64_t findJoltage(std::string_view const bank) {
      29          400 :   uint64_t joltage = 0;
      30          400 :   auto beginIt = bank.begin();
      31          400 :   auto endIt = std::prev(bank.end(), numBatteries - 1);
      32         3200 :   for (int battery = numBatteries - 1; battery >= 0; --battery) {
      33         2800 :     auto it = std::max_element(beginIt, endIt);
      34         2800 :     joltage += ((*it - '0') * powersOf10[battery]);
      35         2800 :     beginIt = std::next(it);
      36         2800 :     ++endIt;
      37         2800 :   }
      38          400 :   return joltage;
      39          400 : }
      40              : 
      41              : } // namespace
      42              : 
      43            1 : template <> std::string solvePart1<2025, 3>(std::string_view const input) {
      44            1 :   return std::to_string(std::ranges::fold_left(
      45            1 :       views::splitLines(input) | views::transform(findJoltage<2>), uint64_t(0), std::plus{}));
      46            1 : }
      47              : 
      48            1 : template <> std::string solvePart2<2025, 3>(std::string_view const input) {
      49            1 :   return std::to_string(std::ranges::fold_left(
      50            1 :       views::splitLines(input) | views::transform(findJoltage<12>), uint64_t(0), std::plus{}));
      51            1 : }
        

Generated by: LCOV version 2.0-1