aboutsummaryrefslogtreecommitdiff
path: root/2017/src/day05.cpp
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2025-11-02 21:41:58 +0100
committerThomas Schmucker <ts@its1.de>2025-11-02 21:41:58 +0100
commit756f22d58bb198b8f34589c112e1003614ccdcd6 (patch)
tree4cdeba335f1ea67f7ead9e9f763ba1c3c206fc4d /2017/src/day05.cpp
parentfcbb722bd4c5ca2bd34a7cf9c0ba48f2f955da13 (diff)
downloadadvent-of-code-756f22d58bb198b8f34589c112e1003614ccdcd6.tar.gz
advent-of-code-756f22d58bb198b8f34589c112e1003614ccdcd6.tar.bz2
advent-of-code-756f22d58bb198b8f34589c112e1003614ccdcd6.zip
aoc 2017, days 1-20
Diffstat (limited to '2017/src/day05.cpp')
-rw-r--r--2017/src/day05.cpp57
1 files changed, 57 insertions, 0 deletions
diff --git a/2017/src/day05.cpp b/2017/src/day05.cpp
new file mode 100644
index 0000000..93656b6
--- /dev/null
+++ b/2017/src/day05.cpp
@@ -0,0 +1,57 @@
1#include <filesystem>
2#include <fstream>
3#include <iostream>
4
5using namespace std;
6
7namespace {
8
9vector<int>
10read_lines(const filesystem::path& filename)
11{
12 ifstream file{ filename };
13 vector<int> lines;
14
15 for ( int value{}; file >> value; ) {
16 lines.emplace_back(value);
17 }
18
19 return lines;
20}
21
22void
23part1(vector<int> values)
24{
25 int steps = 0;
26 for ( size_t ip = 0; ip < values.size(); ) {
27 auto offset = values[ip]++;
28 ip += static_cast<size_t>(offset);
29 ++steps;
30 }
31
32 cout << "Part1: " << steps << '\n';
33}
34
35void
36part2(vector<int> values)
37{
38 int steps = 0;
39 for ( size_t ip = 0; ip < values.size(); ) {
40 auto offset = values[ip];
41 values[ip] += (offset >= 3) ? -1 : 1;
42 ip += static_cast<size_t>(offset);
43 ++steps;
44 }
45
46 cout << "Part2: " << steps << '\n';
47}
48
49} // namespace
50
51int
52main()
53{
54 auto values = read_lines("data/day05.txt");
55 part1(values);
56 part2(values);
57}