-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1.bf
130 lines (100 loc) · 3.41 KB
/
part1.bf
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
An implementation of day 15's hash function in BF tested with Wilfred's bfc compiler
Assumes cells to be unsigned bytes that wrap around but may contain arbitrary
data at startup time
memory layout:
0: condition (ie zero once we reached a newline)
1: input char
2: zero if input char is a comma
3: temporary 1 used for 'if not zero' check of cell 2
4: temporary 0 used for 'if not zero' check of cell 2
5: running hash value
6: temporary for copying stuff around
|
10: temporary for copying stuff around
11: temporary hash value for multiplication
12: temporary 17 for the hash computation
13: last hash value
14: temporary
|
23: temporary
[-]+ set cell 0 to one (the condition)
>>>>>[-]<<<<< zero cell 5 (the hash value)
[ while input has not reached newline (cell 0)
idea: copy cell 5 (the hash value) to cell 13 (the last value)
>>>> > in cell 5 (the hash value)
>[-]< >>>>>>>>[-]<<<<<<<< zero cell 6 and 13
[- >+< >>>>>>>>+<<<<<<<<] copy to cell 6 and 13
> in cell 6
[- <+>] move back to cell 5
<
<<<< <
> in cell 1
, read input char
<[-]> >[-]< zero cells 0 2
[- <+> >+< >>>>+<<<<] add char to cells 0 2 and 5 (the hash value)
idea: copy hash value to cell 11 for multiplication
>>> > in cell 5 (hash value)
>>>>>[-]>[-]<<<<<< zero cells 10 and 11
[- >>>>>+>+<<<<<<] copy hash value to cells 10 and 11
<<< <
cell 5 (hash value) is now zero
>>> >>>> >>>> in cell 12
[-] zero cell
++++ ++++ ++++ ++++ + add 17
idea: multiply into cell 5 (hash value) by repeatedly adding cell 11
[ while not zero (ie repeat 17 times)
< in cell 11 (temporary hash value)
<<[-]>> <[-]> zero cell 9 and 10
[- <<+>> <+>] copy hash value to cell 9 and 10
<< in cell 9
[- >>+<<] move hash value back to cell 11
>>
< in cell 10
[- <<<<<+>>>>>] add hash value to cell 5
>
>
- decrement
]
<<< <<<< <<<<
<
>> in cell 2
cell contains input char
---- ---- ---- ----
---- ---- ---- ----
---- ---- ---- subtract 44 (ASCII value for comma)
idea: check if the cell is zero ie if the input char is a comma
for that we use cells 3 and 4 as temporaries
>[-]< >>[-]<< zero cells 3 and 4
>+< set cell 3 to one
[>-] go to cell 3 if nonzero
> we are now in cell 3 with a one if cell 2 was zero ie we had a comma
in cell 4 with a zero if cell 2 was nonzero ie we had something else
[ if nonzero ie if we had a comma
we are in cell 3
- zero cell
<<< in cell 0
>>>> >>>> >>>> > in cell 13 (the hash value)
. output hash
idea: assign cell 13 (last hash value) to cell 5 (hash value)
<<<<<<<<[-]>[-]>>>>>>> zero cell 5 and 6
[- <<<<<<<<+>+>>>>>>>] copy to cell 5 and 6
< <<<< << in cell 6
[- >>>>>>>+<<<<<<<] move back to cell 13
> >>>> >>
<<<< <<<< <<<< <
>>>> > in cell 5 (the hash value)
[-] zero cell
<<<< <
>>>
> go to cell 4
]
<< go back to cell 2
<<
cell 0 contains input char
---- ---- -- subtract 10 (ASCII value for \n)
we rely on the fact that all other chars we are interested in have higher ASCII values
]
>>> >>>> >>>> in cell 24 (the hash sum)
---- ---- -- subtract 10 (last \n)
. output value
<<< <<<< <<<<