aboutsummaryrefslogtreecommitdiff
path: root/2015/src/day08.cpp
blob: 9cd6e21f12a9f962e0a5c3aa906b1c9ee4772a8c (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

auto
read_file(string_view filename)
{
	fstream        input{ filename };
	vector<string> lines;

	for ( string line; getline(input, line); ) {
		lines.emplace_back(line);
	}

	return lines;
}

void
part1(const vector<string>& lines)
{
	auto count = [](const string& line) -> size_t {
		size_t length = 0;
		auto   iter   = begin(line);

		while ( iter != end(line) ) {
			if ( *iter == '\\' ) {
				++iter;
				if ( *iter == '\\' || *iter == '"' ) {
					++length;
					++iter;
				}
				else if ( *iter == 'x' ) {
					++length;
					iter += 3;
				}
			}
			else {
				++iter;
				++length;
			}
		}
		return length - 2;
	};

	size_t sum = 0;
	for ( const auto& line: lines ) {
		sum += line.size() - count(line);
	}
	cout << sum << endl;
}

void
part2(const vector<string>& lines)
{
	long sum = 0;
	for ( const auto& line: lines ) {
		sum += 2 + count_if(begin(line), end(line), [](auto chr) { return chr == '\\' || chr == '"'; });
	}
	cout << sum << endl;
}

int
main()
{
	auto lines = read_file("data/day08.txt");
	part1(lines);
	part2(lines);
}