aboutsummaryrefslogtreecommitdiff
path: root/2017/src/day01.cpp
blob: f8297e71435dc761a203848bd722be26e84d00f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>

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()
{
	auto captcha = read_line("data/day01.txt");
	part1(captcha);
	part2(captcha);
}