blob: aa1ea42e63ad42cf237c40c9385faaedfd158b01 (
plain)
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
|
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
namespace {
vector<string>
read_file(const filesystem::path& filename)
{
ifstream file{ filename };
vector<string> lines;
for ( string line; getline(file, line); ) {
lines.emplace_back(line);
}
return lines;
}
void
part1(const vector<string>& lines)
{
auto zeros = 0;
auto pos = 50;
for ( const auto& line: lines ) {
auto value = stoi(line.substr(1));
value %= 100;
pos += (line[0] == 'L') ? (100 - value) : value;
if ( pos % 100 == 0 ) {
++zeros;
}
}
cout << "Part 1: " << zeros << '\n';
}
void
part2(const vector<string>& lines)
{
auto zeros = 0;
auto pos = 50;
for ( const auto& line: lines ) {
auto value = stoi(line.substr(1));
while ( value-- > 0 ) {
pos += (line[0] == 'L') ? 99 : 1;
if ( pos % 100 == 0 ) {
++zeros;
}
}
}
cout << "Part 2: " << zeros << '\n';
}
} // namespace
int
main()
{
auto data = read_file("data/day01.txt");
part1(data);
part2(data);
}
|