diff options
Diffstat (limited to 'day03/solution.cpp')
| -rw-r--r-- | day03/solution.cpp | 64 |
1 files changed, 64 insertions, 0 deletions
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 @@ | |||
| 1 | #include <bits/stdc++.h> | ||
| 2 | |||
| 3 | namespace views = std::views; | ||
| 4 | namespace ranges = std::ranges; | ||
| 5 | |||
| 6 | auto parse_input(std::istream& is) { | ||
| 7 | const std::string input = { | ||
| 8 | std::istreambuf_iterator<char>(is), | ||
| 9 | std::istreambuf_iterator<char>() | ||
| 10 | }; | ||
| 11 | |||
| 12 | auto const lines = views::split(input, '\n') | ||
| 13 | | views::filter([](auto&& line) { return not line.empty(); }) | ||
| 14 | | ranges::to<std::vector<std::string>>(); | ||
| 15 | return lines; | ||
| 16 | } | ||
| 17 | |||
| 18 | uint64_t compute_joltage(std::string_view bank, int const njolts) { | ||
| 19 | std::string s_joltage; | ||
| 20 | auto cursor = bank.begin(); | ||
| 21 | for (int i = njolts-1; i >= 0; --i) { | ||
| 22 | size_t const begin = std::distance(bank.begin(), cursor); | ||
| 23 | auto const max_elem = | ||
| 24 | ranges::max_element(bank | ||
| 25 | | views::drop(begin) | ||
| 26 | | views::take(bank.size() - begin - i)); | ||
| 27 | s_joltage += *max_elem; | ||
| 28 | cursor = max_elem + 1; | ||
| 29 | } | ||
| 30 | return std::stoull(s_joltage); | ||
| 31 | } | ||
| 32 | |||
| 33 | void part1(auto input) { | ||
| 34 | // Write first part solution here | ||
| 35 | uint64_t answer = 0; | ||
| 36 | |||
| 37 | for (auto bank : input) { | ||
| 38 | answer += compute_joltage(bank, 2); | ||
| 39 | } | ||
| 40 | |||
| 41 | std::println("{}", answer); | ||
| 42 | } | ||
| 43 | |||
| 44 | void part2(auto input) { | ||
| 45 | // Write second part solution here | ||
| 46 | uint64_t answer = 0; | ||
| 47 | |||
| 48 | for (auto bank : input) { | ||
| 49 | auto const joltage = compute_joltage(bank, 12); | ||
| 50 | answer += compute_joltage(bank, 12); | ||
| 51 | } | ||
| 52 | |||
| 53 | std::println("{}", answer); | ||
| 54 | |||
| 55 | } | ||
| 56 | |||
| 57 | int main() { | ||
| 58 | auto input = parse_input(std::cin); | ||
| 59 | |||
| 60 | part1(input); | ||
| 61 | part2(input); | ||
| 62 | |||
| 63 | return 0; | ||
| 64 | } | ||
