advent2022

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

task.txt (5595B)


      1 --- Day 13: Distress Signal ---
      2 
      3 You climb the hill and again try contacting the Elves. However, you instead receive a signal you weren't expecting: a distress signal.
      4 
      5 Your handheld device must still not be working properly; the packets from the distress signal got decoded out of order. You'll need to re-order the list of received packets (your puzzle input) to decode the message.
      6 
      7 Your list consists of pairs of packets; pairs are separated by a blank line. You need to identify how many pairs of packets are in the right order.
      8 
      9 For example:
     10 
     11 [1,1,3,1,1]
     12 [1,1,5,1,1]
     13 
     14 [[1],[2,3,4]]
     15 [[1],4]
     16 
     17 [9]
     18 [[8,7,6]]
     19 
     20 [[4,4],4,4]
     21 [[4,4],4,4,4]
     22 
     23 [7,7,7,7]
     24 [7,7,7]
     25 
     26 []
     27 [3]
     28 
     29 [[[]]]
     30 [[]]
     31 
     32 [1,[2,[3,[4,[5,6,7]]]],8,9]
     33 [1,[2,[3,[4,[5,6,0]]]],8,9]
     34 
     35 Packet data consists of lists and integers. Each list starts with [, ends with ], and contains zero or more comma-separated values (either integers or other lists). Each packet is always a list and appears on its own line.
     36 
     37 When comparing two values, the first value is called left and the second value is called right. Then:
     38 
     39     If both values are integers, the lower integer should come first. If the left integer is lower than the right integer, the inputs are in the right order. If the left integer is higher than the right integer, the inputs are not in the right order. Otherwise, the inputs are the same integer; continue checking the next part of the input.
     40     If both values are lists, compare the first value of each list, then the second value, and so on. If the left list runs out of items first, the inputs are in the right order. If the right list runs out of items first, the inputs are not in the right order. If the lists are the same length and no comparison makes a decision about the order, continue checking the next part of the input.
     41     If exactly one value is an integer, convert the integer to a list which contains that integer as its only value, then retry the comparison. For example, if comparing [0,0,0] and 2, convert the right value to [2] (a list containing 2); the result is then found by instead comparing [0,0,0] and [2].
     42 
     43 Using these rules, you can determine which of the pairs in the example are in the right order:
     44 
     45 == Pair 1 ==
     46 - Compare [1,1,3,1,1] vs [1,1,5,1,1]
     47   - Compare 1 vs 1
     48   - Compare 1 vs 1
     49   - Compare 3 vs 5
     50     - Left side is smaller, so inputs are in the right order
     51 
     52 == Pair 2 ==
     53 - Compare [[1],[2,3,4]] vs [[1],4]
     54   - Compare [1] vs [1]
     55     - Compare 1 vs 1
     56   - Compare [2,3,4] vs 4
     57     - Mixed types; convert right to [4] and retry comparison
     58     - Compare [2,3,4] vs [4]
     59       - Compare 2 vs 4
     60         - Left side is smaller, so inputs are in the right order
     61 
     62 == Pair 3 ==
     63 - Compare [9] vs [[8,7,6]]
     64   - Compare 9 vs [8,7,6]
     65     - Mixed types; convert left to [9] and retry comparison
     66     - Compare [9] vs [8,7,6]
     67       - Compare 9 vs 8
     68         - Right side is smaller, so inputs are not in the right order
     69 
     70 == Pair 4 ==
     71 - Compare [[4,4],4,4] vs [[4,4],4,4,4]
     72   - Compare [4,4] vs [4,4]
     73     - Compare 4 vs 4
     74     - Compare 4 vs 4
     75   - Compare 4 vs 4
     76   - Compare 4 vs 4
     77   - Left side ran out of items, so inputs are in the right order
     78 
     79 == Pair 5 ==
     80 - Compare [7,7,7,7] vs [7,7,7]
     81   - Compare 7 vs 7
     82   - Compare 7 vs 7
     83   - Compare 7 vs 7
     84   - Right side ran out of items, so inputs are not in the right order
     85 
     86 == Pair 6 ==
     87 - Compare [] vs [3]
     88   - Left side ran out of items, so inputs are in the right order
     89 
     90 == Pair 7 ==
     91 - Compare [[[]]] vs [[]]
     92   - Compare [[]] vs []
     93     - Right side ran out of items, so inputs are not in the right order
     94 
     95 == Pair 8 ==
     96 - Compare [1,[2,[3,[4,[5,6,7]]]],8,9] vs [1,[2,[3,[4,[5,6,0]]]],8,9]
     97   - Compare 1 vs 1
     98   - Compare [2,[3,[4,[5,6,7]]]] vs [2,[3,[4,[5,6,0]]]]
     99     - Compare 2 vs 2
    100     - Compare [3,[4,[5,6,7]]] vs [3,[4,[5,6,0]]]
    101       - Compare 3 vs 3
    102       - Compare [4,[5,6,7]] vs [4,[5,6,0]]
    103         - Compare 4 vs 4
    104         - Compare [5,6,7] vs [5,6,0]
    105           - Compare 5 vs 5
    106           - Compare 6 vs 6
    107           - Compare 7 vs 0
    108             - Right side is smaller, so inputs are not in the right order
    109 
    110 What are the indices of the pairs that are already in the right order? (The first pair has index 1, the second pair has index 2, and so on.) In the above example, the pairs in the right order are 1, 2, 4, and 6; the sum of these indices is 13.
    111 
    112 Determine which pairs of packets are already in the right order. What is the sum of the indices of those pairs?
    113 
    114 Your puzzle answer was 5717.
    115 --- Part Two ---
    116 
    117 Now, you just need to put all of the packets in the right order. Disregard the blank lines in your list of received packets.
    118 
    119 The distress signal protocol also requires that you include two additional divider packets:
    120 
    121 [[2]]
    122 [[6]]
    123 
    124 Using the same rules as before, organize all packets - the ones in your list of received packets as well as the two divider packets - into the correct order.
    125 
    126 For the example above, the result of putting the packets in the correct order is:
    127 
    128 []
    129 [[]]
    130 [[[]]]
    131 [1,1,3,1,1]
    132 [1,1,5,1,1]
    133 [[1],[2,3,4]]
    134 [1,[2,[3,[4,[5,6,0]]]],8,9]
    135 [1,[2,[3,[4,[5,6,7]]]],8,9]
    136 [[1],4]
    137 [[2]]
    138 [3]
    139 [[4,4],4,4]
    140 [[4,4],4,4,4]
    141 [[6]]
    142 [7,7,7]
    143 [7,7,7,7]
    144 [[8,7,6]]
    145 [9]
    146 
    147 Afterward, locate the divider packets. To find the decoder key for this distress signal, you need to determine the indices of the two divider packets and multiply them together. (The first packet is at index 1, the second packet is at index 2, and so on.) In this example, the divider packets are 10th and 14th, and so the decoder key is 140.
    148 
    149 Organize all of the packets into the correct order. What is the decoder key for the distress signal?
    150 
    151 Your puzzle answer was 25935.