4 "cell_type": "markdown",
7 "# Advent of Code 2017\n",
12 " [0 2 7 0] dup max\n"
21 "from notebook_preamble import D, J, V, define"
33 "output_type": "stream",
40 "J('[0 2 7 0] dup max')"
49 "from joy.library import SimpleFunctionWrapper\n",
50 "from joy.utils.stack import list_to_stack\n",
53 "@SimpleFunctionWrapper\n",
54 "def index_of(stack):\n",
55 " '''Given a sequence and a item, return the index of the item, or -1 if not found.\n",
59 " [a b c] a index_of\n",
60 " ------------------------\n",
63 " [a b c] d index_of\n",
64 " ------------------------\n",
68 " item, (sequence, stack) = stack\n",
71 " term, sequence = sequence\n",
72 " if term == item:\n",
80 "D['index_of'] = index_of"
90 "output_type": "stream",
97 "J('[0 2 7 0] 7 index_of')"
102 "execution_count": 5,
107 "output_type": "stream",
114 "J('[0 2 7 0] 23 index_of')"
118 "cell_type": "markdown",
121 "Starting at `index` distribute `count` \"blocks\" to the \"banks\" in the sequence.\n",
123 " [...] count index distribute\n",
124 " ----------------------------\n",
127 "This seems like it would be a PITA to implement in Joypy..."
132 "execution_count": 6,
136 "from joy.utils.stack import iter_stack, list_to_stack\n",
139 "@SimpleFunctionWrapper\n",
140 "def distribute(stack):\n",
141 " '''Starting at index+1 distribute count \"blocks\" to the \"banks\" in the sequence.\n",
143 " [...] count index distribute\n",
144 " ----------------------------\n",
148 " index, (count, (sequence, stack)) = stack\n",
149 " assert count >= 0\n",
150 " cheat = list(iter_stack(sequence))\n",
152 " assert index < n\n",
153 " cheat[index] = 0\n",
157 " cheat[index] += 1\n",
159 " return list_to_stack(cheat), stack\n",
162 "D['distribute'] = distribute"
167 "execution_count": 7,
172 "output_type": "stream",
179 "J('[0 2 7 0] dup max [index_of] nullary distribute')"
184 "execution_count": 8,
189 "output_type": "stream",
196 "J('[2 4 1 2] dup max [index_of] nullary distribute')"
201 "execution_count": 9,
206 "output_type": "stream",
213 "J('[3 1 2 3] dup max [index_of] nullary distribute')"
218 "execution_count": 10,
223 "output_type": "stream",
230 "J('[0 2 3 4] dup max [index_of] nullary distribute')"
235 "execution_count": 11,
240 "output_type": "stream",
247 "J('[1 3 4 1] dup max [index_of] nullary distribute')"
251 "cell_type": "markdown",
254 "### Recalling \"Generator Programs\"\n",
259 " [a F] a swap [C] dip rest cons\n",
260 " a [a F] [C] dip rest cons\n",
261 " a C [a F] rest cons\n",
266 " a dup G [F] cons\n",
269 " w/ G == dup max [index_of] nullary distribute"
274 "execution_count": 12,
278 "define('direco dip rest cons')"
283 "execution_count": 13,
287 "define('G [direco] cons [swap] swoncat cons')"
292 "execution_count": 14,
296 "define('make_distributor [dup dup max [index_of] nullary distribute] G')"
301 "execution_count": 15,
306 "output_type": "stream",
308 "[0 2 7 0] [2 4 1 2] [3 1 2 3] [0 2 3 4] [1 3 4 1] [2 4 1 2]\n"
313 "J('[0 2 7 0] make_distributor 6 [x] times pop')"
317 "cell_type": "markdown",
320 "### A function to drive a generator and count how many states before a repeat.\n",
323 " [] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec\n",
327 " [] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec\n",
328 " [] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec\n",
329 " [] [...] [GEN] pop index_of 0 >=\n",
330 " [] [...] index_of 0 >=\n",
336 " [] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec\n",
337 " [] [...] [GEN] pop size --\n",
338 " [] [...] size --\n",
339 " [] [...] size --\n",
341 "A mistake, `popop` and no need for `--`\n",
343 " [] [...] [GEN] popop size\n",
349 " [] [...] [GEN] [pop index_of 0 >=] [popop size] [[swons] dip x] tailrec\n",
350 " [] [...] [GEN] [swons] dip x F\n",
351 " [] [...] swons [GEN] x F\n",
352 " [[...]] [GEN] x F\n",
353 " [[...]] [...] [GEN] F\n",
355 " [[...]] [...] [GEN] F\n",
357 "What have we learned?\n",
359 " F == [pop index_of 0 >=] [popop size] [[swons] dip x] tailrec"
364 "execution_count": 16,
368 "define('count_states [] swap x [pop index_of 0 >=] [popop size] [[swons] dip x] tailrec')"
373 "execution_count": 17,
377 "define('AoC2017.6 make_distributor count_states')"
382 "execution_count": 18,
387 "output_type": "stream",
394 "J('[0 2 7 0] AoC2017.6')"
399 "execution_count": 19,
406 "output_type": "stream",
413 "J('[1 1 1] AoC2017.6')"
418 "execution_count": 20,
425 "output_type": "stream",
432 "J('[8 0 0 0 0 0] AoC2017.6')"
438 "display_name": "Python 2",
439 "language": "python",
447 "file_extension": ".py",
448 "mimetype": "text/x-python",
450 "nbconvert_exporter": "python",
451 "pygments_lexer": "ipython3",