summaryrefslogtreecommitdiffstats
path: root/day03
diff options
context:
space:
mode:
Diffstat (limited to 'day03')
-rw-r--r--day03/Makefile1
-rw-r--r--day03/solution.cpp64
-rw-r--r--day03/tests/test1.input4
-rw-r--r--day03/tests/test1.output2
4 files changed, 71 insertions, 0 deletions
diff --git a/day03/Makefile b/day03/Makefile
new file mode 100644
index 0000000..2fa98c0
--- /dev/null
+++ b/day03/Makefile
@@ -0,0 +1 @@
include ../aoc.mk
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
3namespace views = std::views;
4namespace ranges = std::ranges;
5
6auto 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
18uint64_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
33void 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
44void 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
57int main() {
58 auto input = parse_input(std::cin);
59
60 part1(input);
61 part2(input);
62
63 return 0;
64}
diff --git a/day03/tests/test1.input b/day03/tests/test1.input
new file mode 100644
index 0000000..7255fca
--- /dev/null
+++ b/day03/tests/test1.input
@@ -0,0 +1,4 @@
1987654321111111
2811111111111119
3234234234234278
4818181911112111
diff --git a/day03/tests/test1.output b/day03/tests/test1.output
new file mode 100644
index 0000000..8e49ff0
--- /dev/null
+++ b/day03/tests/test1.output
@@ -0,0 +1,2 @@
1357
23121910778619