aboutsummaryrefslogtreecommitdiff
path: root/2016/src/day06.cpp
blob: 75592ca4e8acf1bf118bd0bbf26c8eaac04eb264 (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 <map>
#include <regex>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

vector<string>
read_file(const string& filename)
{
	fstream input{ filename };

	vector<string> lines;

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

	return lines;
}

void
solve(const vector<string>& lines)
{
	string part1;
	string part2;

	for ( size_t col = 0; col != lines[0].length(); ++col ) {
		map<char, size_t> counts;
		for ( const auto& line: lines ) {
			++counts[line[col]];
		}

		const auto [min, max] = ranges::minmax_element(counts, [](auto lhs, auto rhs) { return lhs.second < rhs.second; });

		part1 += max->first;
		part2 += min->first;
	}

	cout << part1 << '\n'
	     << part2 << endl;
}

int
main()
{
	auto lines = read_file("data/day06.txt");
	solve(lines);
}