diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-01-03 23:35:54 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-01-03 23:35:54 +0100 |
| commit | 56e890cec0a28c0a485212ccebfaf774235a79a2 (patch) | |
| tree | d9c6241a1aa247e06ab5ba2f6f11967b77d458ec /2023/src/day11.cpp | |
| parent | e9a1cb0441d137d7a26cb303b7e72d3424b7392d (diff) | |
| download | advent-of-code-56e890cec0a28c0a485212ccebfaf774235a79a2.tar.gz advent-of-code-56e890cec0a28c0a485212ccebfaf774235a79a2.tar.bz2 advent-of-code-56e890cec0a28c0a485212ccebfaf774235a79a2.zip | |
prepare for more puzzles ... :)
Diffstat (limited to '2023/src/day11.cpp')
| -rw-r--r-- | 2023/src/day11.cpp | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/2023/src/day11.cpp b/2023/src/day11.cpp new file mode 100644 index 0000000..e1a0882 --- /dev/null +++ b/2023/src/day11.cpp | |||
| @@ -0,0 +1,100 @@ | |||
| 1 | #include <cmath> | ||
| 2 | #include <fstream> | ||
| 3 | #include <iostream> | ||
| 4 | #include <set> | ||
| 5 | #include <string> | ||
| 6 | #include <vector> | ||
| 7 | using namespace std; | ||
| 8 | |||
| 9 | vector<string> | ||
| 10 | read_file(string_view filename) | ||
| 11 | { | ||
| 12 | fstream input{ filename }; | ||
| 13 | vector<string> data; | ||
| 14 | |||
| 15 | for ( string line; getline(input, line); ) { | ||
| 16 | data.emplace_back(line); | ||
| 17 | } | ||
| 18 | |||
| 19 | return data; | ||
| 20 | } | ||
| 21 | |||
| 22 | vector<tuple<long, long>> | ||
| 23 | find_points(const vector<string>& input) | ||
| 24 | { | ||
| 25 | vector<tuple<long, long>> result; | ||
| 26 | |||
| 27 | for ( size_t row = 0; row != input.size(); ++row ) { | ||
| 28 | const auto& line = input[row]; | ||
| 29 | |||
| 30 | for ( size_t col = 0; col != line.size(); ++col ) { | ||
| 31 | if ( line[col] == '#' ) { | ||
| 32 | result.emplace_back(long(row), long(col)); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | return result; | ||
| 38 | } | ||
| 39 | |||
| 40 | void | ||
| 41 | find_empty_rows_and_cols(const vector<string>& input, set<long>& cols, set<long>& rows) | ||
| 42 | { | ||
| 43 | for ( size_t row = 0; row != input.size(); ++row ) { | ||
| 44 | const auto& line = input[row]; | ||
| 45 | if ( line.find('#') == line.npos ) { | ||
| 46 | rows.insert(long(row)); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | for ( size_t col = 0; col != input[0].size(); ++col ) { | ||
| 51 | bool empty_col = true; | ||
| 52 | for ( const auto& line: input ) { | ||
| 53 | if ( line[col] == '#' ) { | ||
| 54 | empty_col = false; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | if ( empty_col ) { | ||
| 58 | cols.insert(long(col)); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | long | ||
| 64 | solve(const vector<string>& input, long scale) | ||
| 65 | { | ||
| 66 | auto points = find_points(input); | ||
| 67 | |||
| 68 | set<long> cols; | ||
| 69 | set<long> rows; | ||
| 70 | |||
| 71 | find_empty_rows_and_cols(input, cols, rows); | ||
| 72 | |||
| 73 | for ( auto& point: points ) { | ||
| 74 | const auto row = get<0>(point) + count_if(rows.begin(), rows.end(), [&](long row) { return row < get<0>(point); }) * (scale - 1); | ||
| 75 | const auto col = get<1>(point) + count_if(cols.begin(), cols.end(), [&](long col) { return col < get<1>(point); }) * (scale - 1); | ||
| 76 | |||
| 77 | point = { row, col }; | ||
| 78 | } | ||
| 79 | |||
| 80 | auto sum = 0L; | ||
| 81 | for ( size_t i = 0; i != points.size(); ++i ) { | ||
| 82 | for ( size_t j = i + 1; j != points.size(); ++j ) { | ||
| 83 | const auto& from = points[i]; | ||
| 84 | const auto& to = points[j]; | ||
| 85 | |||
| 86 | sum += abs(get<0>(from) - get<0>(to)) + abs(get<1>(from) - get<1>(to)); | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | return sum; | ||
| 91 | } | ||
| 92 | |||
| 93 | int | ||
| 94 | main() | ||
| 95 | { | ||
| 96 | const auto input = read_file("data/day11.txt"); | ||
| 97 | |||
| 98 | cout << "Part1: " << solve(input, 2) << endl; | ||
| 99 | cout << "Part2: " << solve(input, 1000000) << endl; | ||
| 100 | } | ||
