11 from notebook_preamble import D, J, V, define
16 J('[0 2 7 0] dup max')
24 from joy.library import SimpleFunctionWrapper
25 from joy.utils.stack import list_to_stack
28 @SimpleFunctionWrapper
30 '''Given a sequence and a item, return the index of the item, or -1 if not found.
35 ------------------------
39 ------------------------
43 item, (sequence, stack) = stack
46 term, sequence = sequence
55 D['index_of'] = index_of
60 J('[0 2 7 0] 7 index_of')
68 J('[0 2 7 0] 23 index_of')
74 Starting at `index` distribute `count` "blocks" to the "banks" in the sequence.
76 [...] count index distribute
77 ----------------------------
80 This seems like it would be a PITA to implement in Joypy...
84 from joy.utils.stack import iter_stack, list_to_stack
87 @SimpleFunctionWrapper
88 def distribute(stack):
89 '''Starting at index+1 distribute count "blocks" to the "banks" in the sequence.
91 [...] count index distribute
92 ----------------------------
96 index, (count, (sequence, stack)) = stack
98 cheat = list(iter_stack(sequence))
107 return list_to_stack(cheat), stack
110 D['distribute'] = distribute
115 J('[0 2 7 0] dup max [index_of] nullary distribute')
123 J('[2 4 1 2] dup max [index_of] nullary distribute')
131 J('[3 1 2 3] dup max [index_of] nullary distribute')
139 J('[0 2 3 4] dup max [index_of] nullary distribute')
147 J('[1 3 4 1] dup max [index_of] nullary distribute')
153 ### Recalling "Generator Programs"
158 [a F] a swap [C] dip rest cons
159 a [a F] [C] dip rest cons
168 w/ G == dup max [index_of] nullary distribute
172 define('direco dip rest cons')
177 define('G [direco] cons [swap] swoncat cons')
182 define('make_distributor [dup dup max [index_of] nullary distribute] G')
187 J('[0 2 7 0] make_distributor 6 [x] times pop')
190 [0 2 7 0] [2 4 1 2] [3 1 2 3] [0 2 3 4] [1 3 4 1] [2 4 1 2]
193 ### A function to drive a generator and count how many states before a repeat.
196 [] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec
200 [] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec
201 [] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec
202 [] [...] [GEN] pop index_of 0 >=
203 [] [...] index_of 0 >=
209 [] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec
210 [] [...] [GEN] pop size --
214 A mistake, `popop` and no need for `--`
216 [] [...] [GEN] popop size
222 [] [...] [GEN] [pop index_of 0 >=] [popop size] [[swons] dip x] tailrec
223 [] [...] [GEN] [swons] dip x F
224 [] [...] swons [GEN] x F
226 [[...]] [...] [GEN] F
228 [[...]] [...] [GEN] F
230 What have we learned?
232 F == [pop index_of 0 >=] [popop size] [[swons] dip x] tailrec
236 define('count_states [] swap x [pop index_of 0 >=] [popop size] [[swons] dip x] tailrec')
241 define('AoC2017.6 make_distributor count_states')
246 J('[0 2 7 0] AoC2017.6')
254 J('[1 1 1] AoC2017.6')
262 J('[8 0 0 0 0 0] AoC2017.6')