|
| 1 | +# Answers |
| 2 | + |
| 3 | +| Part 1 | Part 2 | |
| 4 | +|---------|--------| |
| 5 | +| `38628` | ` ` | |
| 6 | + |
| 7 | +## --- Day 10: Knot Hash --- |
| 8 | + |
| 9 | +You come across some programs that are trying to implement a software emulation of a hash based on knot-tying. The hash these programs are implementing isn't very strong, but you decide to help them anyway. You make a mental note to remind the Elves later not to invent their own cryptographic functions. |
| 10 | + |
| 11 | +This hash function simulates tying a knot in a circle of string with 256 marks on it. Based on the input to be hashed, the function repeatedly selects a span of string, brings the ends together, and gives the span a half-twist to reverse the order of the marks within it. After doing this many times, the order of the marks is used to build the resulting hash. |
| 12 | + |
| 13 | + 4--5 pinch 4 5 4 1 |
| 14 | + / \ 5,0,1 / \/ \ twist / \ / \ |
| 15 | + 3 0 --> 3 0 --> 3 X 0 |
| 16 | + \ / \ /\ / \ / \ / |
| 17 | + 2--1 2 1 2 5 |
| 18 | + |
| 19 | + |
| 20 | +To achieve this, begin with a _list_ of numbers from `0` to `255`, a _current position_ which begins at `0` (the first element in the list), a _skip size_ (which starts at `0`), and a sequence of _lengths_ (your puzzle input). Then, for each length: |
| 21 | + |
| 22 | +* _Reverse_ the order of that _length_ of elements in the _list_, starting with the element at the _current position_. |
| 23 | +* _Move_ the _current position_ forward by that _length_ plus the _skip size_. |
| 24 | +* _Increase_ the _skip size_ by one. |
| 25 | + |
| 26 | +The _list_ is circular; if the _current position_ and the _length_ try to reverse elements beyond the end of the list, the operation reverses using as many extra elements as it needs from the front of the list. If the _current position_ moves past the end of the list, it wraps around to the front. _Lengths_ larger than the size of the _list_ are invalid. |
| 27 | + |
| 28 | +Here's an example using a smaller list: |
| 29 | + |
| 30 | +Suppose we instead only had a circular list containing five elements, `0, 1, 2, 3, 4`, and were given input lengths of `3, 4, 1, 5`. |
| 31 | + |
| 32 | +* The list begins as `[0] 1 2 3 4` (where square brackets indicate the _current position_). |
| 33 | +* The first length, `3`, selects `([0] 1 2) 3 4` (where parentheses indicate the sublist to be reversed). |
| 34 | +* After reversing that section (`0 1 2` into `2 1 0`), we get `([2] 1 0) 3 4`. |
| 35 | +* Then, the _current position_ moves forward by the _length_, `3`, plus the _skip size_, 0: `2 1 0 [3] 4`. Finally, the _skip size_ increases to `1`. |
| 36 | + |
| 37 | +* The second length, `4`, selects a section which wraps: `2 1) 0 ([3] 4`. |
| 38 | +* The sublist `3 4 2 1` is reversed to form `1 2 4 3`: `4 3) 0 ([1] 2`. |
| 39 | +* The _current position_ moves forward by the _length_ plus the _skip size_, a total of `5`, causing it not to move because it wraps around: `4 3 0 [1] 2`. The _skip size_ increases to `2`. |
| 40 | + |
| 41 | +* The third length, `1`, selects a sublist of a single element, and so reversing it has no effect. |
| 42 | +* The _current position_ moves forward by the _length_ (`1`) plus the _skip size_ (`2`): `4 [3] 0 1 2`. The _skip size_ increases to `3`. |
| 43 | + |
| 44 | +* The fourth length, `5`, selects every element starting with the second: `4) ([3] 0 1 2`. Reversing this sublist (`3 0 1 2 4` into `4 2 1 0 3`) produces: `3) ([4] 2 1 0`. |
| 45 | +* Finally, the _current position_ moves forward by `8`: `3 4 2 1 [0]`. The _skip size_ increases to `4`. |
| 46 | + |
| 47 | +In this example, the first two numbers in the list end up being `3` and `4`; to check the process, you can multiply them together to produce `12`. |
| 48 | + |
| 49 | +However, you should instead use the standard list size of `256` (with values `0` to `255`) and the sequence of _lengths_ in your puzzle input. Once this process is complete, _what is the result of multiplying the first two numbers in the list_? |
0 commit comments