Function advent_solutions::advent2017::knot_hash::hash
[−]
[src]
pub fn hash(nums: &mut [u8], lengths: &[u8], rounds: usize)
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:
- Reverse the order of that length of elements in the list, starting with the element at the current position.
- Move the current position forward by that length plus the skip size.
- Increase the skip size by one.
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.
Here's an example using a smaller list:
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.
- The list begins as
[0] 1 2 3 4(where square brackets indicate the current position). - The first length,
3, selects([0] 1 2) 3 4(where parentheses indicate the sublist to be reversed). - After reversing that section (
0 1 2into2 1 0), we get([2] 1 0) 3 4. - 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 to1.
- The second length,
4, selects a section which wraps:2 1) 0 ([3] 4. - The sublist
3 4 2 1is reversed to form1 2 4 3:4 3) 0 ([1] 2. - 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 to2.
- The third length,
1, selects a sublist of a single element, and so reversing it has no effect. - The current position moves forward by the length (
1) plus the skip size (2):4 [3] 0 1 2. The skip size increases to3.
- The fourth length,
5, selects every element starting with the second:4) ([3] 0 1 2. Reversing this sublist (3 0 1 2 4into4 2 1 0 3) produces:3) ([4] 2 1 0. - Finally, the current position moves forward by
8:3 4 2 1 [0]. The skip size increases to4.
let mut nums = knot_hash::new_nums(4); knot_hash::hash(&mut nums, &[3, 4, 1, 5], 1); assert_eq!(nums, &[3, 4, 2, 1, 0]);