aboutsummaryrefslogtreecommitdiff
path: root/2020/src/day05.cpp
blob: 1f5b797bd0affc33c034210f91c080db1f2e32f6 (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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <numeric>
using namespace std;

auto
read_file(string_view filename)
{
	fstream input{ filename };
	return vector<string>{ istream_iterator<string>{ input }, istream_iterator<string>{} };
}

auto
to_int(string_view str)
{
	auto result = 0U;
	for ( auto chr: str ) {
		result <<= 1U;
		if ( chr == 'B' || chr == 'R' ) {
			result |= 1U;
		}
	}
	return result;
}

void
part1(const vector<string>& lines)
{
	auto value = 0U;
	for ( const auto& line: lines ) {
		value = max(value, to_int(line));
	}
	cout << value << endl;
}

void
part2(const vector<string>& lines)
{
	vector<unsigned int> values;
	transform(lines.begin(), lines.end(), back_inserter(values), to_int);

	sort(values.begin(), values.end());

	for ( size_t idx = 1; idx != values.size(); ++idx ) {
		if ( values[idx - 1] != values[idx] - 1 ) {
			cout << values[idx] - 1 << endl;
			break;
		}
	}
}

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