From 756f22d58bb198b8f34589c112e1003614ccdcd6 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 2 Nov 2025 21:41:58 +0100 Subject: aoc 2017, days 1-20 --- 2017/src/day01.cpp | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to '2017/src/day01.cpp') 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 @@ +#include +#include #include +#include using namespace std; +namespace { + +string +read_line(const filesystem::path& filename) +{ + ifstream file{ filename }; + string line; + getline(file, line); + return line; +} + +long +calculate(string_view captcha, size_t offset) +{ + long sum = 0; + for ( size_t i = 0; i != captcha.size(); ++i ) { + if ( captcha[i] == captcha[(i + offset) % captcha.size()] ) { + sum += captcha[i] - '0'; + } + } + return sum; +} + +void +part1(string_view captcha) +{ + cout << "Part1: " << calculate(captcha, 1) << '\n'; +} + +void +part2(string_view captcha) +{ + cout << "Part2: " << calculate(captcha, captcha.size() / 2) << '\n'; +} + +} // namespace + int main() { - cout << "Hallo Welt" << '\n'; + auto captcha = read_line("data/day01.txt"); + part1(captcha); + part2(captcha); } -- cgit v1.3