|
| 1 | +#include "day5/arg_input.hpp" |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | +#include <numeric> |
| 5 | +#include <ranges> |
| 6 | +#include <vector> |
| 7 | + |
| 8 | +namespace ranges = std::ranges; |
| 9 | + |
| 10 | +using tree_t = std::unordered_map<int, std::vector<int>>; |
| 11 | +using cache_t = std::unordered_map<size_t, long long>; |
| 12 | + |
| 13 | +auto traverse_impl(const tree_t& tree, int from, int to, cache_t& cache) -> long long { |
| 14 | + auto cache_key = [](int from, int to) { |
| 15 | + return static_cast<size_t>(from) << 32u | static_cast<size_t>(to); |
| 16 | + }; |
| 17 | + |
| 18 | + if (cache.contains(cache_key(from, to))) // if we've seen path (from, to), use the cached value |
| 19 | + return cache.at(cache_key(from, to)); |
| 20 | + |
| 21 | + // check if the tree contains adapters that will lead to `to` |
| 22 | + if (!tree.contains(to)) |
| 23 | + return 0; |
| 24 | + const auto& down = tree.at(to); |
| 25 | + // for each adapter that leads to `to`, traverse again. If this is the start, count the path `+1`. |
| 26 | + auto ways = std::accumulate(down.cbegin(), down.cend(), 0ll, [&](auto a, auto i) { |
| 27 | + if (i == from) // if at the end, count this is a possible path |
| 28 | + return a + 1; |
| 29 | + return a + traverse_impl(tree, from, i, cache); |
| 30 | + }); |
| 31 | + |
| 32 | + cache[cache_key(from, to)] = ways; |
| 33 | + return ways; |
| 34 | +} |
| 35 | + |
| 36 | +template<typename... Args> |
| 37 | +auto traverse(Args... args) { |
| 38 | + cache_t cache; |
| 39 | + return traverse_impl(std::forward<Args>(args)..., cache); |
| 40 | +} |
| 41 | + |
| 42 | +auto main(int argc, char* argv[]) -> int { |
| 43 | + if (argc != 2) { |
| 44 | + std::cout << "Usage: " << argv[0] << " {path-to-file}" << std::endl; |
| 45 | + return 1; |
| 46 | + } |
| 47 | + |
| 48 | + auto file = get_input(argc, argv); |
| 49 | + |
| 50 | + if (std::holds_alternative<int>(file)) |
| 51 | + return std::get<int>(file); |
| 52 | + |
| 53 | + auto& input = std::get<std::ifstream>(file); |
| 54 | + |
| 55 | + std::vector<long long> adapters; |
| 56 | + for (std::string l; std::getline(input, l); ) |
| 57 | + adapters.push_back(std::stoll(l)); |
| 58 | + |
| 59 | + adapters.push_back(0); // throw the 0-jolts outlet in there |
| 60 | + ranges::sort(adapters); |
| 61 | + adapters.push_back(adapters.back()+3); // and the 3-jolts PC |
| 62 | + |
| 63 | + // in the sorted range, get the difference between adapters |
| 64 | + auto steps = adapters; |
| 65 | + ranges::transform(steps, steps.begin(), [](auto a){ |
| 66 | + static auto prev = a; |
| 67 | + auto diff = a - prev; |
| 68 | + prev = a; |
| 69 | + return diff; |
| 70 | + }); |
| 71 | + |
| 72 | + // count the occurence of 1- and 3-difference steps |
| 73 | + auto occurence = [](const auto& it, const auto& cmp){ |
| 74 | + return ranges::count_if(it, [&cmp](const auto& v) { return v == cmp; }); |
| 75 | + }; |
| 76 | + auto one_diff = occurence(steps, 1), |
| 77 | + three_diff = occurence(steps, 3); |
| 78 | + std::cout << "Part 1: " << one_diff << " 1-jolt diffs, " << three_diff << " 3-jolt diffs: " << one_diff * three_diff << "\n"; |
| 79 | + |
| 80 | + // Part 2: make a tree of which adapters lead to adapter i |
| 81 | + auto adapter_tree = tree_t {}; |
| 82 | + adapter_tree.reserve(adapters.size()); |
| 83 | + |
| 84 | + for (size_t i = 1; i < adapters.size(); i++) { |
| 85 | + auto adapter = adapters[i]; |
| 86 | + auto it = adapters.cbegin() + i; |
| 87 | + // find in the sorted range [i-3, i) the adapter that suits, then all adapters [i-3,i) will suit |
| 88 | + auto least = std::upper_bound(it - std::min(3ul, i), it, adapter - 3 - 1); |
| 89 | + adapter_tree[adapter] = tree_t::value_type::second_type(std::distance(least, it)); |
| 90 | + std::copy(least, it, adapter_tree.at(adapter).begin()); |
| 91 | + } |
| 92 | + |
| 93 | + std::cout << "Part 2: " << traverse(adapter_tree, adapters.front(), adapters.back()) << "\n"; |
| 94 | +} |
0 commit comments