advent2022

Advent of Code 2022 Solutions
git clone git://bsandro.tech/advent2022
Log | Files | Refs | README | LICENSE

task.txt (13072B)


      1 --- Day 9: Rope Bridge ---
      2 
      3 This rope bridge creaks as you walk along it. You aren't sure how old it is, or whether it can even support your weight.
      4 
      5 It seems to support the Elves just fine, though. The bridge spans a gorge which was carved out by the massive river far below you.
      6 
      7 You step carefully; as you do, the ropes stretch and twist. You decide to distract yourself by modeling rope physics; maybe you can even figure out where not to step.
      8 
      9 Consider a rope with a knot at each end; these knots mark the head and the tail of the rope. If the head moves far enough away from the tail, the tail is pulled toward the head.
     10 
     11 Due to nebulous reasoning involving Planck lengths, you should be able to model the positions of the knots on a two-dimensional grid. Then, by following a hypothetical series of motions (your puzzle input) for the head, you can determine how the tail will move.
     12 
     13 Due to the aforementioned Planck lengths, the rope must be quite short; in fact, the head (H) and tail (T) must always be touching (diagonally adjacent and even overlapping both count as touching):
     14 
     15 ....
     16 .TH.
     17 ....
     18 
     19 ....
     20 .H..
     21 ..T.
     22 ....
     23 
     24 ...
     25 .H. (H covers T)
     26 ...
     27 
     28 If the head is ever two steps directly up, down, left, or right from the tail, the tail must also move one step in that direction so it remains close enough:
     29 
     30 .....    .....    .....
     31 .TH.. -> .T.H. -> ..TH.
     32 .....    .....    .....
     33 
     34 ...    ...    ...
     35 .T.    .T.    ...
     36 .H. -> ... -> .T.
     37 ...    .H.    .H.
     38 ...    ...    ...
     39 
     40 Otherwise, if the head and tail aren't touching and aren't in the same row or column, the tail always moves one step diagonally to keep up:
     41 
     42 .....    .....    .....
     43 .....    ..H..    ..H..
     44 ..H.. -> ..... -> ..T..
     45 .T...    .T...    .....
     46 .....    .....    .....
     47 
     48 .....    .....    .....
     49 .....    .....    .....
     50 ..H.. -> ...H. -> ..TH.
     51 .T...    .T...    .....
     52 .....    .....    .....
     53 
     54 You just need to work out where the tail goes as the head follows a series of motions. Assume the head and the tail both start at the same position, overlapping.
     55 
     56 For example:
     57 
     58 R 4
     59 U 4
     60 L 3
     61 D 1
     62 R 4
     63 D 1
     64 L 5
     65 R 2
     66 
     67 This series of motions moves the head right four steps, then up four steps, then left three steps, then down one step, and so on. After each step, you'll need to update the position of the tail if the step means the head is no longer adjacent to the tail. Visually, these motions occur as follows (s marks the starting position as a reference point):
     68 
     69 == Initial State ==
     70 
     71 ......
     72 ......
     73 ......
     74 ......
     75 H.....  (H covers T, s)
     76 
     77 == R 4 ==
     78 
     79 ......
     80 ......
     81 ......
     82 ......
     83 TH....  (T covers s)
     84 
     85 ......
     86 ......
     87 ......
     88 ......
     89 sTH...
     90 
     91 ......
     92 ......
     93 ......
     94 ......
     95 s.TH..
     96 
     97 ......
     98 ......
     99 ......
    100 ......
    101 s..TH.
    102 
    103 == U 4 ==
    104 
    105 ......
    106 ......
    107 ......
    108 ....H.
    109 s..T..
    110 
    111 ......
    112 ......
    113 ....H.
    114 ....T.
    115 s.....
    116 
    117 ......
    118 ....H.
    119 ....T.
    120 ......
    121 s.....
    122 
    123 ....H.
    124 ....T.
    125 ......
    126 ......
    127 s.....
    128 
    129 == L 3 ==
    130 
    131 ...H..
    132 ....T.
    133 ......
    134 ......
    135 s.....
    136 
    137 ..HT..
    138 ......
    139 ......
    140 ......
    141 s.....
    142 
    143 .HT...
    144 ......
    145 ......
    146 ......
    147 s.....
    148 
    149 == D 1 ==
    150 
    151 ..T...
    152 .H....
    153 ......
    154 ......
    155 s.....
    156 
    157 == R 4 ==
    158 
    159 ..T...
    160 ..H...
    161 ......
    162 ......
    163 s.....
    164 
    165 ..T...
    166 ...H..
    167 ......
    168 ......
    169 s.....
    170 
    171 ......
    172 ...TH.
    173 ......
    174 ......
    175 s.....
    176 
    177 ......
    178 ....TH
    179 ......
    180 ......
    181 s.....
    182 
    183 == D 1 ==
    184 
    185 ......
    186 ....T.
    187 .....H
    188 ......
    189 s.....
    190 
    191 == L 5 ==
    192 
    193 ......
    194 ....T.
    195 ....H.
    196 ......
    197 s.....
    198 
    199 ......
    200 ....T.
    201 ...H..
    202 ......
    203 s.....
    204 
    205 ......
    206 ......
    207 ..HT..
    208 ......
    209 s.....
    210 
    211 ......
    212 ......
    213 .HT...
    214 ......
    215 s.....
    216 
    217 ......
    218 ......
    219 HT....
    220 ......
    221 s.....
    222 
    223 == R 2 ==
    224 
    225 ......
    226 ......
    227 .H....  (H covers T)
    228 ......
    229 s.....
    230 
    231 ......
    232 ......
    233 .TH...
    234 ......
    235 s.....
    236 
    237 After simulating the rope, you can count up all of the positions the tail visited at least once. In this diagram, s again marks the starting position (which the tail also visited) and # marks other positions the tail visited:
    238 
    239 ..##..
    240 ...##.
    241 .####.
    242 ....#.
    243 s###..
    244 
    245 So, there are 13 positions the tail visited at least once.
    246 
    247 Simulate your complete hypothetical series of motions. How many positions does the tail of the rope visit at least once?
    248 
    249 Your puzzle answer was 6023.
    250 --- Part Two ---
    251 
    252 A rope snaps! Suddenly, the river is getting a lot closer than you remember. The bridge is still there, but some of the ropes that broke are now whipping toward you as you fall through the air!
    253 
    254 The ropes are moving too quickly to grab; you only have a few seconds to choose how to arch your body to avoid being hit. Fortunately, your simulation can be extended to support longer ropes.
    255 
    256 Rather than two knots, you now must simulate a rope consisting of ten knots. One knot is still the head of the rope and moves according to the series of motions. Each knot further down the rope follows the knot in front of it using the same rules as before.
    257 
    258 Using the same series of motions as the above example, but with the knots marked H, 1, 2, ..., 9, the motions now occur as follows:
    259 
    260 == Initial State ==
    261 
    262 ......
    263 ......
    264 ......
    265 ......
    266 H.....  (H covers 1, 2, 3, 4, 5, 6, 7, 8, 9, s)
    267 
    268 == R 4 ==
    269 
    270 ......
    271 ......
    272 ......
    273 ......
    274 1H....  (1 covers 2, 3, 4, 5, 6, 7, 8, 9, s)
    275 
    276 ......
    277 ......
    278 ......
    279 ......
    280 21H...  (2 covers 3, 4, 5, 6, 7, 8, 9, s)
    281 
    282 ......
    283 ......
    284 ......
    285 ......
    286 321H..  (3 covers 4, 5, 6, 7, 8, 9, s)
    287 
    288 ......
    289 ......
    290 ......
    291 ......
    292 4321H.  (4 covers 5, 6, 7, 8, 9, s)
    293 
    294 == U 4 ==
    295 
    296 ......
    297 ......
    298 ......
    299 ....H.
    300 4321..  (4 covers 5, 6, 7, 8, 9, s)
    301 
    302 ......
    303 ......
    304 ....H.
    305 .4321.
    306 5.....  (5 covers 6, 7, 8, 9, s)
    307 
    308 ......
    309 ....H.
    310 ....1.
    311 .432..
    312 5.....  (5 covers 6, 7, 8, 9, s)
    313 
    314 ....H.
    315 ....1.
    316 ..432.
    317 .5....
    318 6.....  (6 covers 7, 8, 9, s)
    319 
    320 == L 3 ==
    321 
    322 ...H..
    323 ....1.
    324 ..432.
    325 .5....
    326 6.....  (6 covers 7, 8, 9, s)
    327 
    328 ..H1..
    329 ...2..
    330 ..43..
    331 .5....
    332 6.....  (6 covers 7, 8, 9, s)
    333 
    334 .H1...
    335 ...2..
    336 ..43..
    337 .5....
    338 6.....  (6 covers 7, 8, 9, s)
    339 
    340 == D 1 ==
    341 
    342 ..1...
    343 .H.2..
    344 ..43..
    345 .5....
    346 6.....  (6 covers 7, 8, 9, s)
    347 
    348 == R 4 ==
    349 
    350 ..1...
    351 ..H2..
    352 ..43..
    353 .5....
    354 6.....  (6 covers 7, 8, 9, s)
    355 
    356 ..1...
    357 ...H..  (H covers 2)
    358 ..43..
    359 .5....
    360 6.....  (6 covers 7, 8, 9, s)
    361 
    362 ......
    363 ...1H.  (1 covers 2)
    364 ..43..
    365 .5....
    366 6.....  (6 covers 7, 8, 9, s)
    367 
    368 ......
    369 ...21H
    370 ..43..
    371 .5....
    372 6.....  (6 covers 7, 8, 9, s)
    373 
    374 == D 1 ==
    375 
    376 ......
    377 ...21.
    378 ..43.H
    379 .5....
    380 6.....  (6 covers 7, 8, 9, s)
    381 
    382 == L 5 ==
    383 
    384 ......
    385 ...21.
    386 ..43H.
    387 .5....
    388 6.....  (6 covers 7, 8, 9, s)
    389 
    390 ......
    391 ...21.
    392 ..4H..  (H covers 3)
    393 .5....
    394 6.....  (6 covers 7, 8, 9, s)
    395 
    396 ......
    397 ...2..
    398 ..H1..  (H covers 4; 1 covers 3)
    399 .5....
    400 6.....  (6 covers 7, 8, 9, s)
    401 
    402 ......
    403 ...2..
    404 .H13..  (1 covers 4)
    405 .5....
    406 6.....  (6 covers 7, 8, 9, s)
    407 
    408 ......
    409 ......
    410 H123..  (2 covers 4)
    411 .5....
    412 6.....  (6 covers 7, 8, 9, s)
    413 
    414 == R 2 ==
    415 
    416 ......
    417 ......
    418 .H23..  (H covers 1; 2 covers 4)
    419 .5....
    420 6.....  (6 covers 7, 8, 9, s)
    421 
    422 ......
    423 ......
    424 .1H3..  (H covers 2, 4)
    425 .5....
    426 6.....  (6 covers 7, 8, 9, s)
    427 
    428 Now, you need to keep track of the positions the new tail, 9, visits. In this example, the tail never moves, and so it only visits 1 position. However, be careful: more types of motion are possible than before, so you might want to visually compare your simulated rope to the one above.
    429 
    430 Here's a larger example:
    431 
    432 R 5
    433 U 8
    434 L 8
    435 D 3
    436 R 17
    437 D 10
    438 L 25
    439 U 20
    440 
    441 These motions occur as follows (individual steps are not shown):
    442 
    443 == Initial State ==
    444 
    445 ..........................
    446 ..........................
    447 ..........................
    448 ..........................
    449 ..........................
    450 ..........................
    451 ..........................
    452 ..........................
    453 ..........................
    454 ..........................
    455 ..........................
    456 ..........................
    457 ..........................
    458 ..........................
    459 ..........................
    460 ...........H..............  (H covers 1, 2, 3, 4, 5, 6, 7, 8, 9, s)
    461 ..........................
    462 ..........................
    463 ..........................
    464 ..........................
    465 ..........................
    466 
    467 == R 5 ==
    468 
    469 ..........................
    470 ..........................
    471 ..........................
    472 ..........................
    473 ..........................
    474 ..........................
    475 ..........................
    476 ..........................
    477 ..........................
    478 ..........................
    479 ..........................
    480 ..........................
    481 ..........................
    482 ..........................
    483 ..........................
    484 ...........54321H.........  (5 covers 6, 7, 8, 9, s)
    485 ..........................
    486 ..........................
    487 ..........................
    488 ..........................
    489 ..........................
    490 
    491 == U 8 ==
    492 
    493 ..........................
    494 ..........................
    495 ..........................
    496 ..........................
    497 ..........................
    498 ..........................
    499 ..........................
    500 ................H.........
    501 ................1.........
    502 ................2.........
    503 ................3.........
    504 ...............54.........
    505 ..............6...........
    506 .............7............
    507 ............8.............
    508 ...........9..............  (9 covers s)
    509 ..........................
    510 ..........................
    511 ..........................
    512 ..........................
    513 ..........................
    514 
    515 == L 8 ==
    516 
    517 ..........................
    518 ..........................
    519 ..........................
    520 ..........................
    521 ..........................
    522 ..........................
    523 ..........................
    524 ........H1234.............
    525 ............5.............
    526 ............6.............
    527 ............7.............
    528 ............8.............
    529 ............9.............
    530 ..........................
    531 ..........................
    532 ...........s..............
    533 ..........................
    534 ..........................
    535 ..........................
    536 ..........................
    537 ..........................
    538 
    539 == D 3 ==
    540 
    541 ..........................
    542 ..........................
    543 ..........................
    544 ..........................
    545 ..........................
    546 ..........................
    547 ..........................
    548 ..........................
    549 .........2345.............
    550 ........1...6.............
    551 ........H...7.............
    552 ............8.............
    553 ............9.............
    554 ..........................
    555 ..........................
    556 ...........s..............
    557 ..........................
    558 ..........................
    559 ..........................
    560 ..........................
    561 ..........................
    562 
    563 == R 17 ==
    564 
    565 ..........................
    566 ..........................
    567 ..........................
    568 ..........................
    569 ..........................
    570 ..........................
    571 ..........................
    572 ..........................
    573 ..........................
    574 ..........................
    575 ................987654321H
    576 ..........................
    577 ..........................
    578 ..........................
    579 ..........................
    580 ...........s..............
    581 ..........................
    582 ..........................
    583 ..........................
    584 ..........................
    585 ..........................
    586 
    587 == D 10 ==
    588 
    589 ..........................
    590 ..........................
    591 ..........................
    592 ..........................
    593 ..........................
    594 ..........................
    595 ..........................
    596 ..........................
    597 ..........................
    598 ..........................
    599 ..........................
    600 ..........................
    601 ..........................
    602 ..........................
    603 ..........................
    604 ...........s.........98765
    605 .........................4
    606 .........................3
    607 .........................2
    608 .........................1
    609 .........................H
    610 
    611 == L 25 ==
    612 
    613 ..........................
    614 ..........................
    615 ..........................
    616 ..........................
    617 ..........................
    618 ..........................
    619 ..........................
    620 ..........................
    621 ..........................
    622 ..........................
    623 ..........................
    624 ..........................
    625 ..........................
    626 ..........................
    627 ..........................
    628 ...........s..............
    629 ..........................
    630 ..........................
    631 ..........................
    632 ..........................
    633 H123456789................
    634 
    635 == U 20 ==
    636 
    637 H.........................
    638 1.........................
    639 2.........................
    640 3.........................
    641 4.........................
    642 5.........................
    643 6.........................
    644 7.........................
    645 8.........................
    646 9.........................
    647 ..........................
    648 ..........................
    649 ..........................
    650 ..........................
    651 ..........................
    652 ...........s..............
    653 ..........................
    654 ..........................
    655 ..........................
    656 ..........................
    657 ..........................
    658 
    659 Now, the tail (9) visits 36 positions (including s) at least once:
    660 
    661 ..........................
    662 ..........................
    663 ..........................
    664 ..........................
    665 ..........................
    666 ..........................
    667 ..........................
    668 ..........................
    669 ..........................
    670 #.........................
    671 #.............###.........
    672 #............#...#........
    673 .#..........#.....#.......
    674 ..#..........#.....#......
    675 ...#........#.......#.....
    676 ....#......s.........#....
    677 .....#..............#.....
    678 ......#............#......
    679 .......#..........#.......
    680 ........#........#........
    681 .........########.........
    682 
    683 Simulate your complete series of motions on a larger rope with ten knots. How many positions does the tail of the rope visit at least once?
    684 
    685 Your puzzle answer was 2533.