diff options
| author | Thomas Schmucker <ts@its1.de> | 2025-11-02 21:41:58 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2025-11-02 21:41:58 +0100 |
| commit | 756f22d58bb198b8f34589c112e1003614ccdcd6 (patch) | |
| tree | 4cdeba335f1ea67f7ead9e9f763ba1c3c206fc4d /2017/src/day02.cpp | |
| parent | fcbb722bd4c5ca2bd34a7cf9c0ba48f2f955da13 (diff) | |
| download | advent-of-code-756f22d58bb198b8f34589c112e1003614ccdcd6.tar.gz advent-of-code-756f22d58bb198b8f34589c112e1003614ccdcd6.tar.bz2 advent-of-code-756f22d58bb198b8f34589c112e1003614ccdcd6.zip | |
aoc 2017, days 1-20
Diffstat (limited to '2017/src/day02.cpp')
| -rw-r--r-- | 2017/src/day02.cpp | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/2017/src/day02.cpp b/2017/src/day02.cpp new file mode 100644 index 0000000..5f965bd --- /dev/null +++ b/2017/src/day02.cpp | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <filesystem> | ||
| 3 | #include <fstream> | ||
| 4 | #include <iostream> | ||
| 5 | #include <sstream> | ||
| 6 | #include <string> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | using namespace std; | ||
| 10 | |||
| 11 | namespace { | ||
| 12 | |||
| 13 | vector<vector<int>> | ||
| 14 | read_file(const filesystem::path& filename) | ||
| 15 | { | ||
| 16 | ifstream file{ filename }; | ||
| 17 | vector<vector<int>> grid; | ||
| 18 | |||
| 19 | for ( string line; getline(file, line); ) { | ||
| 20 | stringstream strm{ line }; | ||
| 21 | |||
| 22 | vector<int> row; | ||
| 23 | |||
| 24 | for ( int value{}; strm >> value; ) { | ||
| 25 | row.emplace_back(value); | ||
| 26 | } | ||
| 27 | |||
| 28 | grid.emplace_back(row); | ||
| 29 | } | ||
| 30 | |||
| 31 | return grid; | ||
| 32 | } | ||
| 33 | |||
| 34 | void | ||
| 35 | part1(const vector<vector<int>>& grid) | ||
| 36 | { | ||
| 37 | int checksum = 0; | ||
| 38 | for ( const auto& row: grid ) { | ||
| 39 | auto [min, max] = ranges::minmax_element(row); | ||
| 40 | checksum += *max - *min; | ||
| 41 | } | ||
| 42 | cout << "Part1: " << checksum << '\n'; | ||
| 43 | } | ||
| 44 | |||
| 45 | void | ||
| 46 | part2(const vector<vector<int>>& grid) | ||
| 47 | { | ||
| 48 | int checksum = 0; | ||
| 49 | for ( auto row: grid ) { | ||
| 50 | ranges::sort(row, ranges::greater()); | ||
| 51 | |||
| 52 | for ( auto it = row.begin(); it != row.end(); ++it ) { | ||
| 53 | for ( auto it2 = next(it); it2 != row.end(); ++it2 ) { | ||
| 54 | if ( *it % *it2 == 0 ) { | ||
| 55 | checksum += *it / *it2; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
| 60 | cout << "Part2: " << checksum << '\n'; | ||
| 61 | } | ||
| 62 | |||
| 63 | } // namespace | ||
| 64 | |||
| 65 | int | ||
| 66 | main() | ||
| 67 | { | ||
| 68 | auto grid = read_file("data/day02.txt"); | ||
| 69 | part1(grid); | ||
| 70 | part2(grid); | ||
| 71 | } | ||
