summaryrefslogtreecommitdiffstats
path: root/day01/solution.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'day01/solution.cpp')
-rw-r--r--day01/solution.cpp69
1 files changed, 69 insertions, 0 deletions
diff --git a/day01/solution.cpp b/day01/solution.cpp
new file mode 100644
index 0000000..9fb9ec1
--- /dev/null
+++ b/day01/solution.cpp
@@ -0,0 +1,69 @@
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 lines = views::split(input, '\n') | views::filter([](auto line) { return not line.empty(); });
13 std::vector<int> document;
14 for (auto const line : lines) {
15 char direction = line[0];
16 std::string out;
17 ranges::copy(line | views::drop(1), std::back_inserter(out));
18 int length = std::stoi(out);
19 document.push_back(direction == 'R' ? length : -length);
20 }
21 return document;
22}
23
24int const DIAL_MAX = 100;
25
26void part1(auto const& input) {
27 // Write first part solution here
28 int answer = 0;
29 int dial = 50;
30
31 for (auto const offset : input) {
32 dial += offset;
33 dial %= DIAL_MAX;
34 if (dial == 0) {
35 ++answer;
36 }
37 }
38
39 std::println("{}", answer);
40}
41
42void part2(auto const& input) {
43 // Write second part solution here
44 int answer = 0;
45 int dial = 50;
46 int sign = 1;
47
48 for (auto const offset : input) {
49 int old_dial = dial;
50 dial += offset;
51 answer += std::abs(dial / DIAL_MAX);
52 if (old_dial * dial < 0 or dial == 0) {
53 ++answer;
54 }
55 dial %= DIAL_MAX;
56 old_dial = dial;
57 std::cerr << answer << ' ' << dial << '\n';
58 }
59 std::println("{}", answer);
60}
61
62int main() {
63 auto const input = parse_input(std::cin);
64
65 part1(input);
66 part2(input);
67
68 return 0;
69}