diff options
| author | Thomas Schmucker <ts@its1.de> | 2023-12-11 21:06:14 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2023-12-11 21:06:14 +0100 |
| commit | 2e395fff02c6f027844386048ceb6d434a9c5717 (patch) | |
| tree | 11fe5f184afdb0986dbe3b308eed9196f77e603b | |
| parent | 0e5defe3603dde4b4b8e2e3a8eeae24046311ebb (diff) | |
| download | advent-of-code-2e395fff02c6f027844386048ceb6d434a9c5717.tar.gz advent-of-code-2e395fff02c6f027844386048ceb6d434a9c5717.tar.bz2 advent-of-code-2e395fff02c6f027844386048ceb6d434a9c5717.zip | |
Lösung für Tag 11
| -rw-r--r-- | makefile | 3 | ||||
| -rw-r--r-- | src/day11.cpp | 145 |
2 files changed, 147 insertions, 1 deletions
| @@ -9,7 +9,8 @@ all: bin/day01 \ | |||
| 9 | bin/day07 \ | 9 | bin/day07 \ |
| 10 | bin/day08 \ | 10 | bin/day08 \ |
| 11 | bin/day09 \ | 11 | bin/day09 \ |
| 12 | bin/day10 | 12 | bin/day10 \ |
| 13 | bin/day11 | ||
| 13 | 14 | ||
| 14 | bin: | 15 | bin: |
| 15 | mkdir $@ | 16 | mkdir $@ |
diff --git a/src/day11.cpp b/src/day11.cpp new file mode 100644 index 0000000..baa1d9e --- /dev/null +++ b/src/day11.cpp | |||
| @@ -0,0 +1,145 @@ | |||
| 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<string> | ||
| 23 | expand(const vector<string>& input) | ||
| 24 | { | ||
| 25 | vector<string> result; | ||
| 26 | |||
| 27 | for ( const auto& line: input ) { | ||
| 28 | if ( line.find('#') == line.npos ) { | ||
| 29 | result.emplace_back(line); | ||
| 30 | } | ||
| 31 | result.emplace_back(line); | ||
| 32 | } | ||
| 33 | |||
| 34 | for ( size_t col = 0; col != result[0].size(); ++col ) { | ||
| 35 | bool empty_col = true; | ||
| 36 | for ( const auto& line: result ) { | ||
| 37 | if ( line[col] == '#' ) { | ||
| 38 | empty_col = false; | ||
| 39 | } | ||
| 40 | } | ||
| 41 | if ( empty_col ) { | ||
| 42 | for ( auto& line: result ) { | ||
| 43 | line.insert(col, 1, '.'); | ||
| 44 | } | ||
| 45 | ++col; | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | return result; | ||
| 50 | } | ||
| 51 | |||
| 52 | vector<tuple<long, long>> | ||
| 53 | find_points(const vector<string>& input) | ||
| 54 | { | ||
| 55 | vector<tuple<long, long>> result; | ||
| 56 | |||
| 57 | for ( size_t row = 0; row != input.size(); ++row ) { | ||
| 58 | const auto& line = input[row]; | ||
| 59 | |||
| 60 | for ( size_t col = 0; col != line.size(); ++col ) { | ||
| 61 | if ( line[col] == '#' ) { | ||
| 62 | result.emplace_back(long(row), long(col)); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | return result; | ||
| 68 | } | ||
| 69 | |||
| 70 | void | ||
| 71 | solve(const vector<tuple<long, long>>& points) | ||
| 72 | { | ||
| 73 | long sum = 0; | ||
| 74 | for ( size_t i = 0; i != points.size(); ++i ) { | ||
| 75 | for ( size_t j = i + 1; j != points.size(); ++j ) { | ||
| 76 | const auto& from = points[i]; | ||
| 77 | const auto& to = points[j]; | ||
| 78 | |||
| 79 | sum += abs(get<0>(from) - get<0>(to)) + abs(get<1>(from) - get<1>(to)); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | cout << sum << endl; | ||
| 83 | |||
| 84 | } | ||
| 85 | |||
| 86 | void | ||
| 87 | part1() | ||
| 88 | { | ||
| 89 | solve(find_points(expand(read_file("data/day11.txt")))); | ||
| 90 | } | ||
| 91 | |||
| 92 | void | ||
| 93 | find_empty_rows_ans_cols(const vector<string>& input, set<long>& cols, set<long>& rows) | ||
| 94 | { | ||
| 95 | cols.clear(); | ||
| 96 | rows.clear(); | ||
| 97 | |||
| 98 | for ( size_t row = 0; row != input.size(); ++row ) { | ||
| 99 | const auto& line = input[row]; | ||
| 100 | if ( line.find('#') == line.npos ) { | ||
| 101 | rows.insert(long(row)); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | for ( size_t col = 0; col != input[0].size(); ++col ) { | ||
| 106 | bool empty_col = true; | ||
| 107 | for ( const auto& line: input ) { | ||
| 108 | if ( line[col] == '#' ) { | ||
| 109 | empty_col = false; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | if ( empty_col ) { | ||
| 113 | cols.insert(long(col)); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | void | ||
| 119 | part2() | ||
| 120 | { | ||
| 121 | auto input = read_file("data/day11.txt"); | ||
| 122 | auto points = find_points(input); | ||
| 123 | |||
| 124 | set<long> cols; | ||
| 125 | set<long> rows; | ||
| 126 | |||
| 127 | find_empty_rows_ans_cols(input, cols, rows); | ||
| 128 | |||
| 129 | auto scale = 1000000 - 1; | ||
| 130 | for ( auto& point: points ) { | ||
| 131 | const auto row = get<0>(point) + count_if(rows.begin(), rows.end(), [&](long row) { return row < get<0>(point); }) * scale; | ||
| 132 | const auto col = get<1>(point) + count_if(cols.begin(), cols.end(), [&](long col) { return col < get<1>(point); }) * scale; | ||
| 133 | |||
| 134 | point = { row, col }; | ||
| 135 | } | ||
| 136 | |||
| 137 | solve(points); | ||
| 138 | } | ||
| 139 | |||
| 140 | int | ||
| 141 | main() | ||
| 142 | { | ||
| 143 | part1(); | ||
| 144 | part2(); | ||
| 145 | } | ||
