From 6e3d4ab1fe741dc242080c5fe6e438e714ab553c Mon Sep 17 00:00:00 2001 From: Orfeas <38209077+0xfea5@users.noreply.github.com> Date: Sat, 6 Dec 2025 03:33:12 +0200 Subject: day03 --- day03/solution.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 day03/solution.cpp (limited to 'day03/solution.cpp') diff --git a/day03/solution.cpp b/day03/solution.cpp new file mode 100644 index 0000000..d90843f --- /dev/null +++ b/day03/solution.cpp @@ -0,0 +1,64 @@ +#include + +namespace views = std::views; +namespace ranges = std::ranges; + +auto parse_input(std::istream& is) { + const std::string input = { + std::istreambuf_iterator(is), + std::istreambuf_iterator() + }; + + auto const lines = views::split(input, '\n') + | views::filter([](auto&& line) { return not line.empty(); }) + | ranges::to>(); + return lines; +} + +uint64_t compute_joltage(std::string_view bank, int const njolts) { + std::string s_joltage; + auto cursor = bank.begin(); + for (int i = njolts-1; i >= 0; --i) { + size_t const begin = std::distance(bank.begin(), cursor); + auto const max_elem = + ranges::max_element(bank + | views::drop(begin) + | views::take(bank.size() - begin - i)); + s_joltage += *max_elem; + cursor = max_elem + 1; + } + return std::stoull(s_joltage); +} + +void part1(auto input) { + // Write first part solution here + uint64_t answer = 0; + + for (auto bank : input) { + answer += compute_joltage(bank, 2); + } + + std::println("{}", answer); +} + +void part2(auto input) { + // Write second part solution here + uint64_t answer = 0; + + for (auto bank : input) { + auto const joltage = compute_joltage(bank, 12); + answer += compute_joltage(bank, 12); + } + + std::println("{}", answer); + +} + +int main() { + auto input = parse_input(std::cin); + + part1(input); + part2(input); + + return 0; +} -- cgit v1.2.3