aboutsummaryrefslogtreecommitdiff
path: root/2015/src/day05.cpp
blob: b88ecf5f49502290dc12dc00f64f04713f60a197 (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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <regex>
#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)
{
	const regex re1("(.*[aeiou]){3}");
	const regex re2("(.)\\1");
	const regex re3("(ab|cd|pq|xy)");

	auto count = count_if(begin(lines), end(lines), [&](auto line) {
		return regex_search(line, re1) && regex_search(line, re2) && !regex_search(line, re3);
	});

	cout << count << endl;
}

void
part2(const vector<string>& lines)
{
	const regex re1("(..).*\\1");
	const regex re2("(.).\\1");

	auto count = count_if(begin(lines), end(lines), [&](auto line) {
		return regex_search(line, re1) && regex_search(line, re2);
	});

	cout << count << endl;
}

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