blob: 9fb9ec10f0fc829b545a94600ccc8755d83faf54 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
#include <bits/stdc++.h>
namespace views = std::views;
namespace ranges = std::ranges;
auto parse_input(std::istream& is) {
const std::string input = {
std::istreambuf_iterator<char>(is),
std::istreambuf_iterator<char>()
};
auto lines = views::split(input, '\n') | views::filter([](auto line) { return not line.empty(); });
std::vector<int> document;
for (auto const line : lines) {
char direction = line[0];
std::string out;
ranges::copy(line | views::drop(1), std::back_inserter(out));
int length = std::stoi(out);
document.push_back(direction == 'R' ? length : -length);
}
return document;
}
int const DIAL_MAX = 100;
void part1(auto const& input) {
// Write first part solution here
int answer = 0;
int dial = 50;
for (auto const offset : input) {
dial += offset;
dial %= DIAL_MAX;
if (dial == 0) {
++answer;
}
}
std::println("{}", answer);
}
void part2(auto const& input) {
// Write second part solution here
int answer = 0;
int dial = 50;
int sign = 1;
for (auto const offset : input) {
int old_dial = dial;
dial += offset;
answer += std::abs(dial / DIAL_MAX);
if (old_dial * dial < 0 or dial == 0) {
++answer;
}
dial %= DIAL_MAX;
old_dial = dial;
std::cerr << answer << ' ' << dial << '\n';
}
std::println("{}", answer);
}
int main() {
auto const input = parse_input(std::cin);
part1(input);
part2(input);
return 0;
}
|