aboutsummaryrefslogtreecommitdiff
path: root/2020/src
diff options
context:
space:
mode:
Diffstat (limited to '2020/src')
-rw-r--r--2020/src/day05.cpp62
1 files changed, 62 insertions, 0 deletions
diff --git a/2020/src/day05.cpp b/2020/src/day05.cpp
new file mode 100644
index 0000000..1f5b797
--- /dev/null
+++ b/2020/src/day05.cpp
@@ -0,0 +1,62 @@
1#include <algorithm>
2#include <fstream>
3#include <iostream>
4#include <iterator>
5#include <string>
6#include <vector>
7#include <numeric>
8using namespace std;
9
10auto
11read_file(string_view filename)
12{
13 fstream input{ filename };
14 return vector<string>{ istream_iterator<string>{ input }, istream_iterator<string>{} };
15}
16
17auto
18to_int(string_view str)
19{
20 auto result = 0U;
21 for ( auto chr: str ) {
22 result <<= 1U;
23 if ( chr == 'B' || chr == 'R' ) {
24 result |= 1U;
25 }
26 }
27 return result;
28}
29
30void
31part1(const vector<string>& lines)
32{
33 auto value = 0U;
34 for ( const auto& line: lines ) {
35 value = max(value, to_int(line));
36 }
37 cout << value << endl;
38}
39
40void
41part2(const vector<string>& lines)
42{
43 vector<unsigned int> values;
44 transform(lines.begin(), lines.end(), back_inserter(values), to_int);
45
46 sort(values.begin(), values.end());
47
48 for ( size_t idx = 1; idx != values.size(); ++idx ) {
49 if ( values[idx - 1] != values[idx] - 1 ) {
50 cout << values[idx] - 1 << endl;
51 break;
52 }
53 }
54}
55
56int
57main()
58{
59 auto lines = read_file("data/day05.txt");
60 part1(lines);
61 part2(lines);
62}