aboutsummaryrefslogtreecommitdiff
path: root/2017/src/day01.cpp
diff options
context:
space:
mode:
Diffstat (limited to '2017/src/day01.cpp')
-rw-r--r--2017/src/day01.cpp44
1 files changed, 43 insertions, 1 deletions
diff --git a/2017/src/day01.cpp b/2017/src/day01.cpp
index 4a3fd99..f8297e7 100644
--- a/2017/src/day01.cpp
+++ b/2017/src/day01.cpp
@@ -1,9 +1,51 @@
1#include <filesystem>
2#include <fstream>
1#include <iostream> 3#include <iostream>
4#include <string>
2 5
3using namespace std; 6using namespace std;
4 7
8namespace {
9
10string
11read_line(const filesystem::path& filename)
12{
13 ifstream file{ filename };
14 string line;
15 getline(file, line);
16 return line;
17}
18
19long
20calculate(string_view captcha, size_t offset)
21{
22 long sum = 0;
23 for ( size_t i = 0; i != captcha.size(); ++i ) {
24 if ( captcha[i] == captcha[(i + offset) % captcha.size()] ) {
25 sum += captcha[i] - '0';
26 }
27 }
28 return sum;
29}
30
31void
32part1(string_view captcha)
33{
34 cout << "Part1: " << calculate(captcha, 1) << '\n';
35}
36
37void
38part2(string_view captcha)
39{
40 cout << "Part2: " << calculate(captcha, captcha.size() / 2) << '\n';
41}
42
43} // namespace
44
5int 45int
6main() 46main()
7{ 47{
8 cout << "Hallo Welt" << '\n'; 48 auto captcha = read_line("data/day01.txt");
49 part1(captcha);
50 part2(captcha);
9} 51}