|
| 1 | +/****************************************************************************** |
| 2 | + * File: day02.hpp |
| 3 | + * |
| 4 | + * Author: Eric T. Johnson (yut23) |
| 5 | + * Created: 2025-12-02 |
| 6 | + *****************************************************************************/ |
| 7 | + |
| 8 | +#ifndef DAY02_HPP_YMBKSAFO |
| 9 | +#define DAY02_HPP_YMBKSAFO |
| 10 | + |
| 11 | +#include "lib.hpp" |
| 12 | +#include "util/math.hpp" // for next_power_of_10, prev_power_of_10 |
| 13 | +#include <cassert> // for assert |
| 14 | +#include <iostream> // for istream |
| 15 | +#include <string> // for string, getline |
| 16 | + |
| 17 | +namespace aoc::day02 { |
| 18 | + |
| 19 | +long sum_invalid_ids(const std::string &line) { |
| 20 | + constexpr auto POWERS_OF_10 = aoc::math::gen_powers_of_10<long>(); |
| 21 | + |
| 22 | + const char *ptr1 = line.c_str(); |
| 23 | + const char *ptr2 = ptr1; |
| 24 | + // this const_cast is annoying |
| 25 | + long start = std::strtol(ptr1, const_cast<char **>(&ptr2), 10); |
| 26 | + std::size_t start_digits = ptr2 - ptr1; |
| 27 | + assert(*ptr2 == '-'); |
| 28 | + ptr1 = ptr2 + 1; |
| 29 | + long end = std::strtol(ptr1, const_cast<char **>(&ptr2), 10); |
| 30 | + std::size_t end_digits = ptr2 - ptr1; |
| 31 | + if constexpr (aoc::DEBUG) |
| 32 | + std::cerr << "range " << start << "-" << end << ":"; |
| 33 | + |
| 34 | + if (start_digits % 2 == 1 && end_digits % 2 == 1) { |
| 35 | + // all values have an odd number of digits |
| 36 | + if constexpr (aoc::DEBUG) |
| 37 | + std::cerr << "\n"; |
| 38 | + return 0; |
| 39 | + } |
| 40 | + |
| 41 | + if (start_digits % 2 == 1) { |
| 42 | + start = aoc::math::next_power_of_10(start); |
| 43 | + ++start_digits; |
| 44 | + if (start > end) { |
| 45 | + if constexpr (aoc::DEBUG) |
| 46 | + std::cerr << "\n"; |
| 47 | + return 0; |
| 48 | + } |
| 49 | + } |
| 50 | + long start_upper = start / POWERS_OF_10[start_digits / 2]; |
| 51 | + long start_lower = start % POWERS_OF_10[start_digits / 2]; |
| 52 | + |
| 53 | + if (end_digits % 2 == 1) { |
| 54 | + end = aoc::math::prev_power_of_10(end); |
| 55 | + --end_digits; |
| 56 | + if (start > end) { |
| 57 | + if constexpr (aoc::DEBUG) |
| 58 | + std::cerr << "\n"; |
| 59 | + return 0; |
| 60 | + } |
| 61 | + } |
| 62 | + long end_upper = end / POWERS_OF_10[end_digits / 2]; |
| 63 | + long end_lower = end % POWERS_OF_10[end_digits / 2]; |
| 64 | + |
| 65 | + constexpr auto value_for_upper = [](long x) { |
| 66 | + return x * aoc::math::next_power_of_10(x) + x; |
| 67 | + }; |
| 68 | + |
| 69 | + long sum = 0; |
| 70 | + if (start_lower <= start_upper && |
| 71 | + (end_upper > start_upper || end_lower >= start_upper)) { |
| 72 | + auto x = value_for_upper(start_upper); |
| 73 | + if constexpr (aoc::DEBUG) |
| 74 | + std::cerr << " " << x; |
| 75 | + sum += x; |
| 76 | + } |
| 77 | + if (start_upper < end_upper) { |
| 78 | + for (long upper = start_upper + 1; upper <= end_upper - 1; ++upper) { |
| 79 | + auto x = value_for_upper(upper); |
| 80 | + if constexpr (aoc::DEBUG) |
| 81 | + std::cerr << " " << x; |
| 82 | + sum += x; |
| 83 | + } |
| 84 | + if (end_lower >= end_upper) { |
| 85 | + auto x = value_for_upper(end_upper); |
| 86 | + if constexpr (aoc::DEBUG) |
| 87 | + std::cerr << " " << x; |
| 88 | + sum += x; |
| 89 | + } |
| 90 | + } |
| 91 | + if constexpr (aoc::DEBUG) |
| 92 | + std::cerr << std::endl; |
| 93 | + |
| 94 | + return sum; |
| 95 | +} |
| 96 | + |
| 97 | +} // namespace aoc::day02 |
| 98 | + |
| 99 | +#endif /* end of include guard: DAY02_HPP_YMBKSAFO */ |
0 commit comments