aboutsummaryrefslogtreecommitdiff
path: root/2017/src/day01.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/day01.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/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}