-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.p
109 lines (86 loc) · 5.82 KB
/
day10.p
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
--- Day 10: Knot Hash ---
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.
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). 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 2 into 2 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 to 1.
The second length, 4, selects a section which wraps: 2 1) 0 ([3] 4.
The sublist 3 4 2 1 is reversed to form 1 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.
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.
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.
Finally, the current position moves forward by 8: 3 4 2 1 [0]. The skip size increases to 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.
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?
*/
DEFINE VARIABLE cInput AS CHARACTER INITIAL "34,88,2,222,254,93,150,0,199,255,39,32,137,136,1,167" NO-UNDO.
FUNCTION subList RETURNS CHARACTER (pcList AS CHARACTER, piFrom AS INTEGER, piLength AS INTEGER) FORWARD.
FUNCTION reverseList RETURNS CHARACTER (pcList AS CHARACTER) FORWARD.
FUNCTION patchList RETURNS CHARACTER (pcList AS CHARACTER, piFrom AS INTEGER, pcPatch AS CHARACTER) FORWARD.
DEFINE VARIABLE i AS INTEGER NO-UNDO.
DEFINE VARIABLE iLength AS INTEGER NO-UNDO.
DEFINE VARIABLE iSkip AS INTEGER NO-UNDO.
DEFINE VARIABLE iPos AS INTEGER NO-UNDO.
DEFINE VARIABLE cList AS CHARACTER NO-UNDO.
DEFINE VARIABLE cSelect AS CHARACTER NO-UNDO.
DO i = 0 TO 255:
cList = cList + "," + STRING(i).
END.
cList = SUBSTRING(cList,2).
iPos = 1.
DO i = 1 TO NUM-ENTRIES(cInput):
iLength = INTEGER(ENTRY(i,cInput)).
cSelect = reverseList(subList(cList,iPos,iLength)).
cList = patchList(cList,iPos,cSelect).
iPos = (iPos + iLength + iSkip) MODULO 256.
IF iPos = 0 THEN iPos = 256.
iSkip = iSkip + 1.
END.
MESSAGE reverseList(sublist("1,2,3,4,5,6",5,4))
VIEW-AS ALERT-BOX INFO BUTTONS OK.
FUNCTION subList RETURNS CHARACTER (pcList AS CHARACTER, piFrom AS INTEGER, piLength AS INTEGER):
DEFINE VARIABLE iPos AS INTEGER NO-UNDO.
DEFINE VARIABLE i AS INTEGER NO-UNDO.
DEFINE VARIABLE cSub AS CHARACTER NO-UNDO.
iPos = piFrom.
DO i = 1 TO piLength:
cSub = cSub + "," + ENTRY(iPos, pcList).
iPos = (iPos + 1) MODULO NUM-ENTRIES(pcList).
IF iPos = 0 THEN iPos = NUM-ENTRIES(pcList).
END.
RETURN SUBSTRING(cSub,2).
END FUNCTION.
FUNCTION reverseList RETURNS CHARACTER (pcList AS CHARACTER):
DEFINE VARIABLE cRev AS CHARACTER NO-UNDO.
DEFINE VARIABLE i AS INTEGER NO-UNDO.
DO i = NUM-ENTRIES(pcList) TO 1 BY -1:
cRev = cRev + "," + ENTRY(i, pcList).
END.
RETURN SUBSTRING(cRev,2).
END FUNCTION.
FUNCTION patchList RETURNS CHARACTER (pcList AS CHARACTER, piFrom AS INTEGER, pcPatch AS CHARACTER):
DEFINE VARIABLE iPos AS INTEGER NO-UNDO.
DEFINE VARIABLE i AS INTEGER NO-UNDO.
iPos = piFrom.
DO i = 1 TO NUM-ENTRIES(pcPatch):
ENTRY(iPos,pcList) = ENTRY(i,pcPatch).
iPos = (iPos + 1) MODULO NUM-ENTRIES(pcList).
IF iPos = 0 THEN iPos = NUM-ENTRIES(pcList).
END.
RETURN pcList.
END FUNCTION.