aboutsummaryrefslogtreecommitdiff
path: root/2018/src/day01.cpp
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2025-11-16 13:23:34 +0100
committerThomas Schmucker <ts@its1.de>2025-11-16 13:23:34 +0100
commit8c96639ec1f6757570510fc27f1c5fabece35eaf (patch)
tree3a2b2146b6e8a353fa4c5edd105790320cce2b26 /2018/src/day01.cpp
parentbdd35a7bee7f23c912d0c453abc263805d8d8ccf (diff)
downloadadvent-of-code-8c96639ec1f6757570510fc27f1c5fabece35eaf.tar.gz
advent-of-code-8c96639ec1f6757570510fc27f1c5fabece35eaf.tar.bz2
advent-of-code-8c96639ec1f6757570510fc27f1c5fabece35eaf.zip
aoc 2018, days 1-11
Diffstat (limited to '2018/src/day01.cpp')
-rw-r--r--2018/src/day01.cpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/2018/src/day01.cpp b/2018/src/day01.cpp
new file mode 100644
index 0000000..d57a187
--- /dev/null
+++ b/2018/src/day01.cpp
@@ -0,0 +1,58 @@
1#include <filesystem>
2#include <fstream>
3#include <iostream>
4#include <numeric>
5#include <unordered_set>
6#include <vector>
7
8using namespace std;
9
10namespace {
11
12vector<long>
13read_file(const filesystem::path& filename)
14{
15 ifstream file{ filename };
16 vector<long> values;
17
18 for ( long value = 0; file >> value; ) {
19 values.emplace_back(value);
20 }
21
22 return values;
23}
24
25void
26part1(const vector<long>& values)
27{
28 auto sum = accumulate(values.begin(), values.end(), 0L);
29 cout << "Part1: " << sum << '\n';
30}
31
32void
33part2(const vector<long>& values)
34{
35 unordered_set<long> seen;
36
37 long sum = 0;
38 for ( size_t idx = 0;; ++idx ) {
39 sum += values.at(idx % values.size());
40
41 if ( seen.contains(sum) ) {
42 cout << "Part2: " << sum << '\n';
43 return;
44 }
45
46 seen.emplace(sum);
47 }
48}
49
50} // namespace
51
52int
53main()
54{
55 auto values = read_file("data/day01.txt");
56 part1(values);
57 part2(values);
58}