13 from notebook_preamble import D, J, V, define
17 J('[0 2 7 0] dup max')
27 from joy.library import SimpleFunctionWrapper
28 from joy.utils.stack import list_to_stack
31 @SimpleFunctionWrapper
33 '''Given a sequence and a item, return the index of the item, or -1 if not found.
38 ------------------------
42 ------------------------
46 item, (sequence, stack) = stack
49 term, sequence = sequence
58 D['index_of'] = index_of
62 J('[0 2 7 0] 7 index_of')
72 J('[0 2 7 0] 23 index_of')
80 Starting at ``index`` distribute ``count`` "blocks" to the "banks" in
85 [...] count index distribute
86 ----------------------------
89 This seems like it would be a PITA to implement in Joypy...
93 from joy.utils.stack import iter_stack, list_to_stack
96 @SimpleFunctionWrapper
97 def distribute(stack):
98 '''Starting at index+1 distribute count "blocks" to the "banks" in the sequence.
100 [...] count index distribute
101 ----------------------------
105 index, (count, (sequence, stack)) = stack
107 cheat = list(iter_stack(sequence))
116 return list_to_stack(cheat), stack
119 D['distribute'] = distribute
123 J('[0 2 7 0] dup max [index_of] nullary distribute')
133 J('[2 4 1 2] dup max [index_of] nullary distribute')
143 J('[3 1 2 3] dup max [index_of] nullary distribute')
153 J('[0 2 3 4] dup max [index_of] nullary distribute')
163 J('[1 3 4 1] dup max [index_of] nullary distribute')
171 Recalling "Generator Programs"
172 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
179 [a F] a swap [C] dip rest cons
180 a [a F] [C] dip rest cons
189 w/ G == dup max [index_of] nullary distribute
193 define('direco dip rest cons')
197 define('G [direco] cons [swap] swoncat cons')
201 define('make_distributor [dup dup max [index_of] nullary distribute] G')
205 J('[0 2 7 0] make_distributor 6 [x] times pop')
210 [0 2 7 0] [2 4 1 2] [3 1 2 3] [0 2 3 4] [1 3 4 1] [2 4 1 2]
213 A function to drive a generator and count how many states before a repeat.
214 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
220 [] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec
226 [] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec
227 [] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec
228 [] [...] [GEN] pop index_of 0 >=
229 [] [...] index_of 0 >=
237 [] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec
238 [] [...] [GEN] pop size --
242 A mistake, ``popop`` and no need for ``--``
246 [] [...] [GEN] popop size
254 [] [...] [GEN] [pop index_of 0 >=] [popop size] [[swons] dip x] tailrec
255 [] [...] [GEN] [swons] dip x F
256 [] [...] swons [GEN] x F
258 [[...]] [...] [GEN] F
260 [[...]] [...] [GEN] F
262 What have we learned?
266 F == [pop index_of 0 >=] [popop size] [[swons] dip x] tailrec
270 define('count_states [] swap x [pop index_of 0 >=] [popop size] [[swons] dip x] tailrec')
274 define('AoC2017.6 make_distributor count_states')
278 J('[0 2 7 0] AoC2017.6')
288 J('[1 1 1] AoC2017.6')
298 J('[8 0 0 0 0 0] AoC2017.6')