diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-01-05 00:19:27 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-01-05 00:19:27 +0100 |
| commit | b3bcb56b9ff48b78c7e1b5d6e9c38df44df7a049 (patch) | |
| tree | aabbd3d116662854bff78af513015da5a101136a | |
| parent | 1f93e69adfbe8830c0ef47a24e07a7f333168dd4 (diff) | |
| download | advent-of-code-b3bcb56b9ff48b78c7e1b5d6e9c38df44df7a049.tar.gz advent-of-code-b3bcb56b9ff48b78c7e1b5d6e9c38df44df7a049.tar.bz2 advent-of-code-b3bcb56b9ff48b78c7e1b5d6e9c38df44df7a049.zip | |
solution for day 5
| -rw-r--r-- | 2020/src/day05.cpp | 62 |
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> | ||
| 8 | using namespace std; | ||
| 9 | |||
| 10 | auto | ||
| 11 | read_file(string_view filename) | ||
| 12 | { | ||
| 13 | fstream input{ filename }; | ||
| 14 | return vector<string>{ istream_iterator<string>{ input }, istream_iterator<string>{} }; | ||
| 15 | } | ||
| 16 | |||
| 17 | auto | ||
| 18 | to_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 | |||
| 30 | void | ||
| 31 | part1(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 | |||
| 40 | void | ||
| 41 | part2(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 | |||
| 56 | int | ||
| 57 | main() | ||
| 58 | { | ||
| 59 | auto lines = read_file("data/day05.txt"); | ||
| 60 | part1(lines); | ||
| 61 | part2(lines); | ||
| 62 | } | ||
