4 ...a list of the offsets for each jump. Jumps are relative: -1 moves to the previous instruction, and 2 skips the next one. Start at the first instruction in the list. The goal is to follow the jumps until one leads outside the list.
6 In addition, these instructions are a little strange; after each jump, the offset of that instruction increases by 1. So, if you come across an offset of 3, you would move three instructions forward, but change it to a 4 for the next time it is encountered.
8 For example, consider the following list of jump offsets:
16 Positive jumps ("forward") move downward; negative jumps move upward. For legibility in this example, these offset values will be written all on one line, with the current instruction marked in parentheses. The following steps would be taken before an exit is found:
18 * (0) 3 0 1 -3 - before we have taken any steps.
19 * (1) 3 0 1 -3 - jump with offset 0 (that is, don't jump at all). Fortunately, the instruction is then incremented to 1.
20 * 2 (3) 0 1 -3 - step forward because of the instruction we just modified. The first instruction is incremented again, now to 2.
21 * 2 4 0 1 (-3) - jump all the way to the end; leave a 4 behind.
22 * 2 (4) 0 1 -2 - go back to where we just were; increment -3 to -2.
23 * 2 5 0 1 -2 - jump 4 steps forward, escaping the maze.
25 In this example, the exit is reached in 5 steps.
27 How many steps does it take to reach the exit?
30 For now, I'm going to assume a starting state with the size of the sequence pre-computed. We need it to define the exit condition and it is a trivial preamble to generate it. We then need and `index` and a `step-count`, which are both initially zero. Then we have the sequence itself, and some recursive function `F` that does the work.
32 size index step-count [...] F
33 -----------------------------------
36 F == [P] [T] [R1] [R2] genrec
38 Later on I was thinking about it and the Forth heuristic came to mind, to wit: four things on the stack are kind of much. Immediately I realized that the size properly belongs in the predicate of `F`! D'oh!
40 index step-count [...] F
41 ------------------------------
44 So, let's start by nailing down the predicate:
46 F == [P] [T] [R1] [R2] genrec
47 == [P] [T] [R1 [F] R2] ifte
49 0 0 [0 3 0 1 -3] popop 5 >=
53 Now we need the else-part:
55 index step-count [0 3 0 1 -3] roll< popop
59 Last but not least, the recursive branch
61 0 0 [0 3 0 1 -3] R1 [F] R2
63 The `R1` function has a big job:
65 R1 == get the value at index
66 increment the value at the index
67 add the value gotten to the index
68 increment the step count
70 The only tricky thing there is incrementing an integer in the sequence. Joy sequences are not particularly good for random access. We could encode the list of jump offsets in a big integer and use math to do the processing for a good speed-up, but it still wouldn't beat the performance of e.g. a mutable array. This is just one of those places where "plain vanilla" Joypy doesn't shine (in default performance. The legendary *Sufficiently-Smart Compiler* would of course rewrite this function to use an array "under the hood".)
72 In the meantime, I'm going to write a primitive function that just does what we need.
76 from notebook_preamble import D, J, V, define
77 from joy.library import SimpleFunctionWrapper
78 from joy.utils.stack import list_to_stack
81 @SimpleFunctionWrapper
83 '''Given a index and a sequence of integers, increment the integer at the index.
87 3 [0 1 2 3 4 5] incr_at
88 -----------------------------
92 sequence, (i, stack) = stack
95 term, sequence = sequence
99 return list_to_stack(mem, sequence), stack
102 D['incr_at'] = incr_at
107 J('3 [0 1 2 3 4 5] incr_at')
113 ### get the value at index
115 3 0 [0 1 2 3 4] [roll< at] nullary
118 ### increment the value at the index
120 3 0 [0 1 2 n 4] n [Q] dip
122 3 0 [0 1 2 n 4] [popd incr_at] unary n
125 ### add the value gotten to the index
127 3 0 [0 1 2 n+1 4] n [+] cons dipd
128 3 0 [0 1 2 n+1 4] [n +] dipd
129 3 n + 0 [0 1 2 n+1 4]
132 ### increment the step count
134 3+n 0 [0 1 2 n+1 4] [++] dip
137 ### All together now...
139 get_value == [roll< at] nullary
140 incr_value == [[popd incr_at] unary] dip
141 add_value == [+] cons dipd
142 incr_step_count == [++] dip
144 R1 == get_value incr_value add_value incr_step_count
146 F == [P] [T] [R1] primrec
148 F == [popop !size! >=] [roll< pop] [get_value incr_value add_value incr_step_count] tailrec
152 from joy.library import DefinitionWrapper
155 DefinitionWrapper.add_definitions('''
157 get_value [roll< at] nullary
158 incr_value [[popd incr_at] unary] dip
159 add_value [+] cons dipd
160 incr_step_count [++] dip
162 AoC2017.5.0 get_value incr_value add_value incr_step_count
169 from joy.library import DefinitionWrapper
172 DefinitionWrapper.add_definitions('''
174 get_value [roll< at] nullary
175 incr_value [[popd incr_at] unary] dip
176 add_value [+] cons dipd
177 incr_step_count [++] dip
179 AoC2017.5.0 get_value incr_value add_value incr_step_count
186 define('F [popop 5 >=] [roll< popop] [AoC2017.5.0] tailrec')
191 J('0 0 [0 3 0 1 -3] F')
197 ### Preamble for setting up predicate, `index`, and `step-count`
199 We want to go from this to this:
201 [...] AoC2017.5.preamble
202 ------------------------------
203 0 0 [...] [popop n >=]
205 Where `n` is the size of the sequence.
207 The first part is obviously `0 0 roll<`, then `dup size`:
209 [...] 0 0 roll< dup size
214 0 0 [...] n [>=] cons [popop] swoncat
218 init-index-and-step-count == 0 0 roll<
219 prepare-predicate == dup size [>=] cons [popop] swoncat
221 AoC2017.5.preamble == init-index-and-step-count prepare-predicate
225 DefinitionWrapper.add_definitions('''
227 init-index-and-step-count 0 0 roll<
228 prepare-predicate dup size [>=] cons [popop] swoncat
230 AoC2017.5.preamble init-index-and-step-count prepare-predicate
232 AoC2017.5 AoC2017.5.preamble [roll< popop] [AoC2017.5.0] tailrec
239 J('[0 3 0 1 -3] AoC2017.5')
246 AoC2017.5 == AoC2017.5.preamble [roll< popop] [AoC2017.5.0] primrec
248 AoC2017.5.0 == get_value incr_value add_value incr_step_count
249 AoC2017.5.preamble == init-index-and-step-count prepare-predicate
251 get_value == [roll< at] nullary
252 incr_value == [[popd incr_at] unary] dip
253 add_value == [+] cons dipd
254 incr_step_count == [++] dip
256 init-index-and-step-count == 0 0 roll<
257 prepare-predicate == dup size [>=] cons [popop] swoncat
260 This is by far the largest program I have yet written in Joy. Even with the `incr_at` function it is still a bear. There may be an arrangement of the parameters that would permit more elegant definitions, but it still wouldn't be as efficient as something written in assembly, C, or even Python.