aboutsummaryrefslogtreecommitdiff
path: root/2015/src/day01.cpp
blob: 5d6d482b4fe3d3cdf0437d29fbcd26986fc8c6e1 (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
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

string
read_file(string_view filename)
{
	fstream input{ filename };
	return { istreambuf_iterator<char>{ input }, {} };
}

void
part1(string_view puzzle)
{
	int floor = 0;
	for (const auto& chr : puzzle) {
		if (chr == '(') {
			++floor;
		}
		else if (chr == ')') {
			--floor;
		}
	}
	cout << floor << endl;
}

void
part2(string_view puzzle)
{
	int floor = 0;
	for (string_view::size_type pos = 0; pos != puzzle.length(); ++pos) {
		const auto chr = puzzle[pos];
		if (chr == '(') {
			++floor;
		}
		else if (chr == ')') {
			--floor;
		}
		if (floor == -1) {
			cout << pos+1 << endl;
			break;
		}
	}
}

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