diff options
Diffstat (limited to 'src/day21.cpp')
| -rw-r--r-- | src/day21.cpp | 74 |
1 files changed, 62 insertions, 12 deletions
diff --git a/src/day21.cpp b/src/day21.cpp index 91611c5..740c3ae 100644 --- a/src/day21.cpp +++ b/src/day21.cpp | |||
| @@ -47,7 +47,7 @@ find_neighbours(position pos, const vector<string>& lines) | |||
| 47 | const auto nrow = row + drow; | 47 | const auto nrow = row + drow; |
| 48 | const auto ncol = col + dcol; | 48 | const auto ncol = col + dcol; |
| 49 | 49 | ||
| 50 | if ( nrow < lines.size() && ncol < lines[0].size() && lines[nrow][ncol] == '.' ) { | 50 | if ( nrow < lines.size() && ncol < lines[0].size() && (lines[nrow][ncol] == '.' || lines[nrow][ncol] == 'S') ) { |
| 51 | neighbours.emplace(nrow, ncol); | 51 | neighbours.emplace(nrow, ncol); |
| 52 | } | 52 | } |
| 53 | } | 53 | } |
| @@ -55,17 +55,14 @@ find_neighbours(position pos, const vector<string>& lines) | |||
| 55 | return neighbours; | 55 | return neighbours; |
| 56 | } | 56 | } |
| 57 | 57 | ||
| 58 | void | 58 | size_t |
| 59 | part1() | 59 | count(vector<string> lines, position start, size_t rounds) |
| 60 | { | 60 | { |
| 61 | auto lines = read_file("data/day21.txt"); | ||
| 62 | auto start_position = find_start_position(lines); | ||
| 63 | |||
| 64 | queue<position> positions; | 61 | queue<position> positions; |
| 65 | positions.emplace(start_position); | 62 | positions.emplace(start); |
| 66 | 63 | ||
| 67 | long sum = 0; | 64 | size_t sum = 0; |
| 68 | for ( int round = 0; round != 64; ++round ) { | 65 | for ( size_t round = 0; round != rounds; ++round ) { |
| 69 | set<position> next_positions; | 66 | set<position> next_positions; |
| 70 | 67 | ||
| 71 | sum = 0; | 68 | sum = 0; |
| @@ -73,7 +70,7 @@ part1() | |||
| 73 | const auto [curr_row, curr_col] = positions.front(); | 70 | const auto [curr_row, curr_col] = positions.front(); |
| 74 | lines[curr_row][curr_col] = '.'; | 71 | lines[curr_row][curr_col] = '.'; |
| 75 | 72 | ||
| 76 | auto neighbours = find_neighbours(positions.front(), lines); | 73 | const auto neighbours = find_neighbours(positions.front(), lines); |
| 77 | positions.pop(); | 74 | positions.pop(); |
| 78 | 75 | ||
| 79 | for ( const auto& [row, col]: neighbours ) { | 76 | for ( const auto& [row, col]: neighbours ) { |
| @@ -87,11 +84,64 @@ part1() | |||
| 87 | positions.emplace(position); | 84 | positions.emplace(position); |
| 88 | } | 85 | } |
| 89 | } | 86 | } |
| 90 | cout << sum << endl; | 87 | return sum; |
| 88 | } | ||
| 89 | |||
| 90 | void | ||
| 91 | part1(const vector<string>& lines) | ||
| 92 | { | ||
| 93 | const auto start = find_start_position(lines); | ||
| 94 | cout << count(lines, start, 64) << endl; | ||
| 95 | } | ||
| 96 | |||
| 97 | void | ||
| 98 | part2(const vector<string>& lines) | ||
| 99 | { | ||
| 100 | const auto start = find_start_position(lines); | ||
| 101 | const auto [row, col] = start; | ||
| 102 | |||
| 103 | const auto pow2 = [](size_t val) -> size_t { | ||
| 104 | return val * val; | ||
| 105 | }; | ||
| 106 | |||
| 107 | const auto size = lines.size(); | ||
| 108 | const size_t steps = 26501365; | ||
| 109 | const auto grid_width = steps / size - 1; | ||
| 110 | |||
| 111 | const auto odd = pow2(grid_width / 2 * 2 + 1); | ||
| 112 | const auto even = pow2((grid_width + 1) / 2 * 2); | ||
| 113 | |||
| 114 | const auto odd_points = count(lines, start, size * 2 + 1); | ||
| 115 | const auto even_points = count(lines, start, size * 2); | ||
| 116 | |||
| 117 | const auto corner_t = count(lines, { size - 1, col }, size - 1); | ||
| 118 | const auto corner_r = count(lines, { row, 0 }, size - 1); | ||
| 119 | const auto corner_b = count(lines, { 0, col }, size - 1); | ||
| 120 | const auto corner_l = count(lines, { row, size - 1 }, size - 1); | ||
| 121 | |||
| 122 | const auto small_tr = count(lines, { size - 1, 0 }, size / 2 - 1); | ||
| 123 | const auto small_tl = count(lines, { size - 1, size - 1 }, size / 2 - 1); | ||
| 124 | const auto small_br = count(lines, { 0, 0 }, size / 2 - 1); | ||
| 125 | const auto small_bl = count(lines, { 0, size - 1 }, size / 2 - 1); | ||
| 126 | |||
| 127 | const auto large_tr = count(lines, { size - 1, 0 }, size * 3 / 2 - 1); | ||
| 128 | const auto large_tl = count(lines, { size - 1, size - 1 }, size * 3 / 2 - 1); | ||
| 129 | const auto large_br = count(lines, { 0, 0 }, size * 3 / 2 - 1); | ||
| 130 | const auto large_bl = count(lines, { 0, size - 1 }, size * 3 / 2 - 1); | ||
| 131 | |||
| 132 | auto total = odd * odd_points; | ||
| 133 | total += even * even_points; | ||
| 134 | total += corner_t + corner_r + corner_b + corner_l; | ||
| 135 | total += (grid_width + 1) * (small_tr + small_tl + small_br + small_bl); | ||
| 136 | total += grid_width * (large_tr + large_tl + large_br + large_bl); | ||
| 137 | |||
| 138 | cout << total << endl; | ||
| 91 | } | 139 | } |
| 92 | 140 | ||
| 93 | int | 141 | int |
| 94 | main() | 142 | main() |
| 95 | { | 143 | { |
| 96 | part1(); | 144 | auto lines = read_file("data/day21.txt"); |
| 145 | part1(lines); | ||
| 146 | part2(lines); | ||
| 97 | } | 147 | } |
