aboutsummaryrefslogtreecommitdiff
path: root/2017/src/day04.cpp
blob: 01e89b2bd5d4097bd1cde58a7313a3a8fbfd438e (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
72
73
74
75
76
77
78
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <set>
#include <sstream>
#include <string>

using namespace std;

namespace {

vector<string>
read_lines(const filesystem::path& filename)
{
	ifstream       file{ filename };
	vector<string> lines;

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

	return lines;
}

vector<string>
split(const string& line)
{
	stringstream   strm{ line };
	vector<string> words;

	for ( string word; strm >> word; ) {
		words.emplace_back(word);
	}

	return words;
}

void
part1(const vector<string>& lines)
{
	auto num = ranges::count_if(lines, [](const string& line) {
		auto        words = split(line);
		set<string> set{ words.begin(), words.end() };

		return set.size() == words.size();
	});

	cout << "Part1: " << num << '\n';
}

void
part2(const vector<string>& lines)
{
	auto num = ranges::count_if(lines, [](const string& line) {
		auto        words = split(line);
		set<string> set;

		for ( auto word: words ) {
			ranges::sort(word);
			set.insert(word);
		}

		return set.size() == words.size();
	});

	cout << "Part2: " << num << '\n';
}

} // namespace

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