diff options
| -rw-r--r-- | 2024/src/day09.cpp | 22 | ||||
| -rw-r--r-- | 2024/src/day15.cpp | 72 |
2 files changed, 28 insertions, 66 deletions
diff --git a/2024/src/day09.cpp b/2024/src/day09.cpp index 7787c5c..55281e0 100644 --- a/2024/src/day09.cpp +++ b/2024/src/day09.cpp | |||
| @@ -106,28 +106,6 @@ part2(const vector<int>& data) | |||
| 106 | } | 106 | } |
| 107 | } | 107 | } |
| 108 | 108 | ||
| 109 | /* | ||
| 110 | for ( auto& file: std::ranges::reverse_view(files) ) { | ||
| 111 | auto space = ranges::find_if(spaces, [&](const auto& space) -> bool { | ||
| 112 | return get<1>(space) >= get<2>(file) && get<0>(space) < get<1>(file); | ||
| 113 | }); | ||
| 114 | if ( space != spaces.end() ) { | ||
| 115 | // update pos | ||
| 116 | get<1>(file) = get<0>(*space); | ||
| 117 | |||
| 118 | // update space len | ||
| 119 | get<1>(*space) -= get<2>(file); | ||
| 120 | |||
| 121 | // update space pos | ||
| 122 | get<0>(*space) += get<2>(file); | ||
| 123 | |||
| 124 | if ( get<1>(*space) == 0 ) { | ||
| 125 | spaces.erase(space); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | */ | ||
| 130 | |||
| 131 | long sum = 0; | 109 | long sum = 0; |
| 132 | for ( const auto& [fid, pos, size]: files ) { | 110 | for ( const auto& [fid, pos, size]: files ) { |
| 133 | for ( size_t x = pos; x != pos + size; ++x ) { | 111 | for ( size_t x = pos; x != pos + size; ++x ) { |
diff --git a/2024/src/day15.cpp b/2024/src/day15.cpp index 94b8a2b..5e29711 100644 --- a/2024/src/day15.cpp +++ b/2024/src/day15.cpp | |||
| @@ -55,43 +55,26 @@ read_file(string_view filename) | |||
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | puzzle2_type | 57 | puzzle2_type |
| 58 | read_file2(string_view filename) | 58 | convert(const puzzle_type& puzzle) |
| 59 | { | 59 | { |
| 60 | fstream input{ filename }; | 60 | const auto& [room, boxes, movements, start] = puzzle; |
| 61 | room_type room; | ||
| 62 | boxes2_type boxes; | ||
| 63 | string movements; | ||
| 64 | pos_type start; | ||
| 65 | 61 | ||
| 66 | size_t yPos = 0; | 62 | room_type room2; |
| 67 | for ( string line; getline(input, line); ) { | 63 | for ( const auto& [x, y]: room ) { |
| 68 | if ( line.empty() ) { | 64 | room2.emplace(x * 2, y); |
| 69 | continue; | 65 | room2.emplace(x * 2 + 1, y); |
| 70 | } | 66 | } |
| 71 | 67 | ||
| 72 | if ( line[0] == '#' ) { | 68 | boxes2_type boxes2; |
| 73 | for ( size_t xPos = 0; xPos != line.size(); ++xPos ) { | 69 | for ( const auto& [x, y]: boxes ) { |
| 74 | auto chr = line.at(xPos); | 70 | boxes2.insert({ pos_type{ x * 2, y }, true }); |
| 75 | if ( chr == '#' ) { | 71 | boxes2.insert({ pos_type{ x * 2 + 1, y }, false }); |
| 76 | room.emplace(xPos * 2, yPos); | ||
| 77 | room.emplace(xPos * 2 + 1, yPos); | ||
| 78 | } | ||
| 79 | else if ( chr == 'O' ) { | ||
| 80 | boxes.insert({ pos_type(xPos * 2, yPos), true }); | ||
| 81 | boxes.insert({ pos_type(xPos * 2 + 1, yPos), false }); | ||
| 82 | } | ||
| 83 | else if ( chr == '@' ) { | ||
| 84 | start = { xPos * 2, yPos }; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | ++yPos; | ||
| 88 | } | ||
| 89 | else { | ||
| 90 | movements += line; | ||
| 91 | } | ||
| 92 | } | 72 | } |
| 93 | 73 | ||
| 94 | return { room, boxes, movements, start }; | 74 | const auto& [x, y] = start; |
| 75 | pos_type start2{ x * 2, y }; | ||
| 76 | |||
| 77 | return { room2, boxes2, movements, start2 }; | ||
| 95 | } | 78 | } |
| 96 | 79 | ||
| 97 | void | 80 | void |
| @@ -181,7 +164,7 @@ can_move(const room_type& room, const boxes_type& boxes, pos_type start, pos_typ | |||
| 181 | void | 164 | void |
| 182 | part1(const puzzle_type& puzzle) | 165 | part1(const puzzle_type& puzzle) |
| 183 | { | 166 | { |
| 184 | map<char, pos_type> deltas = { | 167 | const map<char, pos_type> deltas = { |
| 185 | { '<', { -1, 0 } }, // Left | 168 | { '<', { -1, 0 } }, // Left |
| 186 | { '^', { 0, -1 } }, // Up | 169 | { '^', { 0, -1 } }, // Up |
| 187 | { '>', { 1, 0 } }, // Right | 170 | { '>', { 1, 0 } }, // Right |
| @@ -191,7 +174,7 @@ part1(const puzzle_type& puzzle) | |||
| 191 | auto [room, boxes, movements, start] = puzzle; | 174 | auto [room, boxes, movements, start] = puzzle; |
| 192 | 175 | ||
| 193 | for ( auto chr: movements ) { | 176 | for ( auto chr: movements ) { |
| 194 | const auto delta = deltas[chr]; | 177 | const auto delta = deltas.at(chr); |
| 195 | if ( auto bitw = can_move(room, boxes, start, delta) ) { | 178 | if ( auto bitw = can_move(room, boxes, start, delta) ) { |
| 196 | const auto [dx, dy] = delta; | 179 | const auto [dx, dy] = delta; |
| 197 | 180 | ||
| @@ -228,12 +211,13 @@ get_both_parts(const boxes2_type& boxes, pos_type pos) | |||
| 228 | optional<boxes2_type> | 211 | optional<boxes2_type> |
| 229 | can_move(const room_type& room, const boxes2_type& all_boxes, pos_type start, pos_type direction) // NOLINT | 212 | can_move(const room_type& room, const boxes2_type& all_boxes, pos_type start, pos_type direction) // NOLINT |
| 230 | { | 213 | { |
| 231 | function<optional<boxes2_type>(const boxes2_type&, size_t)> can_move_up_down = [&room, &all_boxes, &can_move_up_down](const boxes2_type& boxes, size_t dy) -> optional<boxes2_type> { | 214 | function<optional<boxes2_type>(const boxes2_type&, size_t)> can_move_up_down = |
| 215 | [&room, &all_boxes, &can_move_up_down](const boxes2_type& boxes, const size_t dy) -> optional<boxes2_type> { | ||
| 232 | boxes2_type candidates; | 216 | boxes2_type candidates; |
| 233 | 217 | ||
| 234 | for ( const auto& [pos, b]: boxes ) { | 218 | for ( const auto& [pos, b]: boxes ) { |
| 235 | const auto [x, y] = pos; | 219 | const auto [x, y] = pos; |
| 236 | pos_type try_pos(x, y + dy); | 220 | const pos_type try_pos(x, y + dy); |
| 237 | 221 | ||
| 238 | if ( room.contains(try_pos) ) { | 222 | if ( room.contains(try_pos) ) { |
| 239 | return nullopt; | 223 | return nullopt; |
| @@ -290,7 +274,7 @@ can_move(const room_type& room, const boxes2_type& all_boxes, pos_type start, po | |||
| 290 | void | 274 | void |
| 291 | part2(const puzzle2_type& puzzle) | 275 | part2(const puzzle2_type& puzzle) |
| 292 | { | 276 | { |
| 293 | map<char, pos_type> deltas = { | 277 | const map<char, pos_type> deltas = { |
| 294 | { '<', { -1, 0 } }, // Left | 278 | { '<', { -1, 0 } }, // Left |
| 295 | { '^', { 0, -1 } }, // Up | 279 | { '^', { 0, -1 } }, // Up |
| 296 | { '>', { 1, 0 } }, // Right | 280 | { '>', { 1, 0 } }, // Right |
| @@ -300,7 +284,7 @@ part2(const puzzle2_type& puzzle) | |||
| 300 | auto [room, boxes, movements, start] = puzzle; | 284 | auto [room, boxes, movements, start] = puzzle; |
| 301 | 285 | ||
| 302 | for ( auto chr: movements ) { | 286 | for ( auto chr: movements ) { |
| 303 | const auto delta = deltas[chr]; | 287 | const auto delta = deltas.at(chr); |
| 304 | if ( auto bitw = can_move(room, boxes, start, delta) ) { | 288 | if ( auto bitw = can_move(room, boxes, start, delta) ) { |
| 305 | const auto [dx, dy] = delta; | 289 | const auto [dx, dy] = delta; |
| 306 | 290 | ||
| @@ -320,15 +304,15 @@ part2(const puzzle2_type& puzzle) | |||
| 320 | 304 | ||
| 321 | cout << accumulate(boxes.begin(), boxes.end(), 0UL, [](auto init, const auto& box) { | 305 | cout << accumulate(boxes.begin(), boxes.end(), 0UL, [](auto init, const auto& box) { |
| 322 | const auto& [pos, b] = box; | 306 | const auto& [pos, b] = box; |
| 323 | if ( !b ) { | 307 | const auto [x, y] = pos; |
| 324 | return init; | 308 | return init + (x + 100 * y) * b; |
| 325 | } | ||
| 326 | return init + get<0>(pos) + 100 * get<1>(pos); | ||
| 327 | }) << endl; | 309 | }) << endl; |
| 328 | } | 310 | } |
| 311 | |||
| 329 | int | 312 | int |
| 330 | main() | 313 | main() |
| 331 | { | 314 | { |
| 332 | part1(read_file("data/day15.txt")); | 315 | const auto puzzle = read_file("data/day15.txt"); |
| 333 | part2(read_file2("data/day15.txt")); | 316 | part1(puzzle); |
| 317 | part2(convert(puzzle)); | ||
| 334 | } | 318 | } |
