Advent of Code Day 10 结哈希

解题语言不限Java

拖更了,不好意思

题目内容

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.
你走在路上,看到一些程序正在尝试执行一个用结哈希来进行的软件仿真。这些程序生成的哈希鲁棒性并不高,但是你决定去帮助他们。你暗暗记下回去是告诉精灵他们的加密程序并不强。
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.
这个哈希程序模拟了一个有256个元素的环装结构打结的过程。基于输入,这个程序重复选择一段字符串,并且将其反向。多次循环过后,环装结构的元素会被用来建立哈希。

  4--5   pinch   4  5           4   1
 /    \  5,0,1  / \/ \  twist  / \ / \
3      0  -->  3      0  -->  3   X   0
 \    /         \ /\ /         \ / \ /
  2--1           2  1           2   5

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).
为了从一个从0到255的数组达到这种效果,需要一个当前位置(从零开始),一个跳跃大小(从零开始)和一组长度(谜题输入)。
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.
这个列表是循环的,如果当前位置大于数组长度(256),当前位置会被重置到0。
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.
假设数组只有5个元素大0,1,2,3,4,并且输入长度为3,4,1,5
The list begins as [0] 1 2 3 4 (where square brackets indicate the current position).
这个例子从[0] 1 2 3 4,(中括号指代当前位置在数组中的位置)。
The first length, 3, selects ([0] 1 2) 3 4 (where parentheses indicate the sublist to be reversed).
第一个长度是3,选择([0] 1 2) 3 4(小括号代表要被翻转的数组)
After reversing that section (0 1 2 into 2 1 0), we get ([2] 1 0) 3 4.
在第一次翻转之后(0 1 22 1 0),我们可以得到([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 to 1.
在之后,当前位置向前移动三加上跳跃大小02 1 0 [3] 4。最后,跳跃大小加一。
The second length, 4, selects a section which wraps: 2 1) 0 ([3] 4.
第二个长度是4,选择2 1) 0 ([3] 4
The sublist 3 4 2 1 is reversed to form 1 2 4 3: 4 3) 0 ([1] 2.
翻转指定部分之后(3 4 2 11 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 to 2.
在之后,当前位置向前移动五,到达数组结尾并返回:4 3 0 [1] 2。最后,跳跃大小加到二。
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 to 3.
第三个长度是1,选择一个元素,所以没有效果。当前位置向前移动34 [3] 0 1 2,跳跃大小加到三。
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.
第二个长度是5,选择每一个在数组中的元素4) ([3] 0 1 2,翻转它(3 0 1 2 44 2 1 0 3) 3) ([4] 2 1 0.
Finally, the current position moves forward by 8: 3 4 2 1 [0]. The skip size increases to 4.
最后当前位置移动8,跳跃大小变为4。
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.
在例子里,数组前两个的乘积为(3×4)12。

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?
但是,你的数组有256个位长,长度列表为你的谜题输入。当序列完成之后,头两个元素的乘积是多少?

解题思路

鉴于我手上还有Day 11 和Day 12 的没有搞完,大家就等等吧hhh

    原文作者:宅纸箱
    原文地址: https://www.jianshu.com/p/24319b3772c1
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞