5 ...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.
7 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.
9 For example, consider the following list of jump offsets:
17 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:
19 * (0) 3 0 1 -3 - before we have taken any steps.
20 * (1) 3 0 1 -3 - jump with offset 0 (that is, don't jump at all). Fortunately, the instruction is then incremented to 1.
21 * 2 (3) 0 1 -3 - step forward because of the instruction we just modified. The first instruction is incremented again, now to 2.
22 * 2 4 0 1 (-3) - jump all the way to the end; leave a 4 behind.
23 * 2 (4) 0 1 -2 - go back to where we just were; increment -3 to -2.
24 * 2 5 0 1 -2 - jump 4 steps forward, escaping the maze.
26 In this example, the exit is reached in 5 steps.
28 How many steps does it take to reach the exit?
31 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.
33 size index step-count [...] F
34 -----------------------------------
37 F == [P] [T] [R1] [R2] genrec
39 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!
41 index step-count [...] F
42 ------------------------------
45 So, let's start by nailing down the predicate:
47 F == [P] [T] [R1] [R2] genrec
48 == [P] [T] [R1 [F] R2] ifte
50 0 0 [0 3 0 1 -3] popop 5 >=
54 Now we need the else-part:
56 index step-count [0 3 0 1 -3] roll< popop
60 Last but not least, the recursive branch
62 0 0 [0 3 0 1 -3] R1 [F] R2
64 The `R1` function has a big job:
66 R1 == get the value at index
67 increment the value at the index
68 add the value gotten to the index
69 increment the step count
71 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".)
73 In the meantime, I'm going to write a primitive function that just does what we need.
77 from notebook_preamble import D, J, V, define
78 from joy.library import SimpleFunctionWrapper
79 from joy.utils.stack import list_to_stack
82 @SimpleFunctionWrapper
84 '''Given a index and a sequence of integers, increment the integer at the index.
88 3 [0 1 2 3 4 5] incr_at
89 -----------------------------
93 sequence, (i, stack) = stack
96 term, sequence = sequence
100 return list_to_stack(mem, sequence), stack
103 D['incr_at'] = incr_at
108 J('3 [0 1 2 3 4 5] incr_at')
114 ### get the value at index
116 3 0 [0 1 2 3 4] [roll< at] nullary
119 ### increment the value at the index
121 3 0 [0 1 2 n 4] n [Q] dip
123 3 0 [0 1 2 n 4] [popd incr_at] unary n
126 ### add the value gotten to the index
128 3 0 [0 1 2 n+1 4] n [+] cons dipd
129 3 0 [0 1 2 n+1 4] [n +] dipd
130 3 n + 0 [0 1 2 n+1 4]
133 ### increment the step count
135 3+n 0 [0 1 2 n+1 4] [++] dip
138 ### All together now...
140 get_value == [roll< at] nullary
141 incr_value == [[popd incr_at] unary] dip
142 add_value == [+] cons dipd
143 incr_step_count == [++] dip
145 R1 == get_value incr_value add_value incr_step_count
147 F == [P] [T] [R1] primrec
149 F == [popop !size! >=] [roll< pop] [get_value incr_value add_value incr_step_count] primrec
153 from joy.library import DefinitionWrapper
156 DefinitionWrapper.add_definitions('''
158 get_value == [roll< at] nullary
159 incr_value == [[popd incr_at] unary] dip
160 add_value == [+] cons dipd
161 incr_step_count == [++] dip
163 AoC2017.5.0 == get_value incr_value add_value incr_step_count
170 define('F == [popop 5 >=] [roll< popop] [AoC2017.5.0] primrec')
175 J('0 0 [0 3 0 1 -3] F')
181 ### Preamble for setting up predicate, `index`, and `step-count`
183 We want to go from this to this:
185 [...] AoC2017.5.preamble
186 ------------------------------
187 0 0 [...] [popop n >=]
189 Where `n` is the size of the sequence.
191 The first part is obviously `0 0 roll<`, then `dup size`:
193 [...] 0 0 roll< dup size
198 0 0 [...] n [>=] cons [popop] swoncat
202 init-index-and-step-count == 0 0 roll<
203 prepare-predicate == dup size [>=] cons [popop] swoncat
205 AoC2017.5.preamble == init-index-and-step-count prepare-predicate
209 DefinitionWrapper.add_definitions('''
211 init-index-and-step-count == 0 0 roll<
212 prepare-predicate == dup size [>=] cons [popop] swoncat
214 AoC2017.5.preamble == init-index-and-step-count prepare-predicate
216 AoC2017.5 == AoC2017.5.preamble [roll< popop] [AoC2017.5.0] primrec
223 J('[0 3 0 1 -3] AoC2017.5')
230 AoC2017.5 == AoC2017.5.preamble [roll< popop] [AoC2017.5.0] primrec
232 AoC2017.5.0 == get_value incr_value add_value incr_step_count
233 AoC2017.5.preamble == init-index-and-step-count prepare-predicate
235 get_value == [roll< at] nullary
236 incr_value == [[popd incr_at] unary] dip
237 add_value == [+] cons dipd
238 incr_step_count == [++] dip
240 init-index-and-step-count == 0 0 roll<
241 prepare-predicate == dup size [>=] cons [popop] swoncat
244 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.