|
| 1 | +--- Day 14: Disk Defragmentation --- |
| 2 | + |
| 3 | +Suddenly, a scheduled job activates the system's disk defragmenter. Were |
| 4 | +the situation different, you might sit and watch it for a while, but |
| 5 | +today, you just don't have that kind of time. It's soaking up valuable |
| 6 | +system resources that are needed elsewhere, and so the only option is to |
| 7 | +help it finish its task as soon as possible. |
| 8 | + |
| 9 | +The disk in question consists of a 128x128 grid; each square of the grid |
| 10 | +is either free or used. On this disk, the state of the grid is tracked |
| 11 | +by the bits in a sequence of knot hashes. |
| 12 | + |
| 13 | +A total of 128 knot hashes are calculated, each corresponding to a |
| 14 | +single row in the grid; each hash contains 128 bits which correspond to |
| 15 | +individual grid squares. Each bit of a hash indicates whether that |
| 16 | +square is free (0) or used (1). |
| 17 | + |
| 18 | +The hash inputs are a key string (your puzzle input), a dash, and a |
| 19 | +number from 0 to 127 corresponding to the row. For example, if your key |
| 20 | +string were flqrgnkx, then the first row would be given by the bits of |
| 21 | +the knot hash of flqrgnkx-0, the second row from the bits of the knot |
| 22 | +hash of flqrgnkx-1, and so on until the last row, flqrgnkx-127. |
| 23 | + |
| 24 | +The output of a knot hash is traditionally represented by 32 hexadecimal |
| 25 | +digits; each of these digits correspond to 4 bits, for a total of 4 * 32 |
| 26 | += 128 bits. To convert to bits, turn each hexadecimal digit to its |
| 27 | +equivalent binary value, high-bit first: 0 becomes 0000, 1 becomes 0001, |
| 28 | +e becomes 1110, f becomes 1111, and so on; a hash that begins with |
| 29 | +a0c2017... in hexadecimal would begin with |
| 30 | +10100000110000100000000101110000... in binary. |
| 31 | + |
| 32 | +Continuing this process, the first 8 rows and columns for key flqrgnkx |
| 33 | +appear as follows, using # to denote used squares, and . to denote free |
| 34 | +ones: |
| 35 | + |
| 36 | +##.#.#..--> |
| 37 | +.#.#.#.# |
| 38 | +....#.#. |
| 39 | +#.#.##.# |
| 40 | +.##.#... |
| 41 | +##..#..# |
| 42 | +.#...#.. |
| 43 | +##.#.##.--> |
| 44 | +| | |
| 45 | +V V |
| 46 | + |
| 47 | +In this example, 8108 squares are used across the entire 128x128 grid. |
| 48 | + |
| 49 | +Given your actual key string, how many squares are used? |
| 50 | + |
| 51 | +Your puzzle answer was 8190. |
| 52 | + |
| 53 | +--- Part Two --- |
| 54 | + |
| 55 | +Now, all the defragmenter needs to know is the number of regions. A |
| 56 | +region is a group of used squares that are all adjacent, not including |
| 57 | +diagonals. Every used square is in exactly one region: lone used squares |
| 58 | +form their own isolated regions, while several adjacent squares all |
| 59 | +count as a single region. |
| 60 | + |
| 61 | +In the example above, the following nine regions are visible, each |
| 62 | +marked with a distinct digit: |
| 63 | + |
| 64 | +11.2.3..--> |
| 65 | +.1.2.3.4 |
| 66 | +....5.6. |
| 67 | +7.8.55.9 |
| 68 | +.88.5... |
| 69 | +88..5..8 |
| 70 | +.8...8.. |
| 71 | +88.8.88.--> |
| 72 | +| | |
| 73 | +V V |
| 74 | + |
| 75 | +Of particular interest is the region marked 8; while it does not appear |
| 76 | +contiguous in this small view, all of the squares marked 8 are connected |
| 77 | +when considering the whole 128x128 grid. In total, in this example, 1242 |
| 78 | +regions are present. |
| 79 | + |
| 80 | +How many regions are present given your key string? |
| 81 | + |
| 82 | +Your puzzle answer was 1134. |
| 83 | + |
0 commit comments