14 from notebook_preamble import D, J, V, define
18 J('[0 2 7 0] dup max')
28 from joy.library import SimpleFunctionWrapper
29 from joy.utils.stack import list_to_stack
32 @SimpleFunctionWrapper
34 '''Given a sequence and a item, return the index of the item, or -1 if not found.
39 ------------------------
43 ------------------------
47 item, (sequence, stack) = stack
50 term, sequence = sequence
59 D['index_of'] = index_of
63 J('[0 2 7 0] 7 index_of')
73 J('[0 2 7 0] 23 index_of')
81 Starting at ``index`` distribute ``count`` "blocks" to the "banks" in
86 [...] count index distribute
87 ----------------------------
90 This seems like it would be a PITA to implement in Joypy...
94 from joy.utils.stack import iter_stack, list_to_stack
97 @SimpleFunctionWrapper
98 def distribute(stack):
99 '''Starting at index+1 distribute count "blocks" to the "banks" in the sequence.
101 [...] count index distribute
102 ----------------------------
106 index, (count, (sequence, stack)) = stack
108 cheat = list(iter_stack(sequence))
117 return list_to_stack(cheat), stack
120 D['distribute'] = distribute
124 J('[0 2 7 0] dup max [index_of] nullary distribute')
134 J('[2 4 1 2] dup max [index_of] nullary distribute')
144 J('[3 1 2 3] dup max [index_of] nullary distribute')
154 J('[0 2 3 4] dup max [index_of] nullary distribute')
164 J('[1 3 4 1] dup max [index_of] nullary distribute')
172 Recalling "Generator Programs"
173 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
180 [a F] a swap [C] dip rest cons
181 a [a F] [C] dip rest cons
190 w/ G == dup max [index_of] nullary distribute
194 define('direco == dip rest cons')
198 define('G == [direco] cons [swap] swoncat cons')
202 define('make_distributor == [dup dup max [index_of] nullary distribute] G')
206 J('[0 2 7 0] make_distributor 6 [x] times pop')
211 [0 2 7 0] [2 4 1 2] [3 1 2 3] [0 2 3 4] [1 3 4 1] [2 4 1 2]
214 A function to drive a generator and count how many states before a repeat.
215 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
221 [] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec
227 [] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec
228 [] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec
229 [] [...] [GEN] pop index_of 0 >=
230 [] [...] index_of 0 >=
238 [] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec
239 [] [...] [GEN] pop size --
243 A mistake, ``popop`` and no need for ``--``
247 [] [...] [GEN] popop size
255 [] [...] [GEN] [pop index_of 0 >=] [popop size] [[swons] dip x] primrec
256 [] [...] [GEN] [swons] dip x F
257 [] [...] swons [GEN] x F
259 [[...]] [...] [GEN] F
261 [[...]] [...] [GEN] F
263 What have we learned?
267 F == [pop index_of 0 >=] [popop size] [[swons] dip x] primrec
271 define('count_states == [] swap x [pop index_of 0 >=] [popop size] [[swons] dip x] primrec')
275 define('AoC2017.6 == make_distributor count_states')
279 J('[0 2 7 0] AoC2017.6')
289 J('[1 1 1] AoC2017.6')
299 J('[8 0 0 0 0 0] AoC2017.6')