4 "cell_type": "markdown",
7 "# Advent of Code 2017\n",
11 "For each row, determine the difference between the largest value and the smallest value; the checksum is the sum of all of these differences.\n",
13 "For example, given the following spreadsheet:\n",
19 "* The first row's largest and smallest values are 9 and 1, and their difference is 8.\n",
20 "* The second row's largest and smallest values are 7 and 3, and their difference is 4.\n",
21 "* The third row's difference is 6.\n",
23 "In this example, the spreadsheet's checksum would be 8 + 4 + 6 = 18."
32 "from notebook_preamble import J, V, define"
36 "cell_type": "markdown",
39 "I'll assume the input is a Joy sequence of sequences of integers.\n",
45 "So, obviously, the initial form will be a `step` function:\n",
47 " AoC2017.2 == 0 swap [F +] step"
51 "cell_type": "markdown",
54 "This function `F` must get the `max` and `min` of a row of numbers and subtract. We can define a helper function `maxmin` which does this:"
63 "define('maxmin [max] [min] cleave')"
73 "output_type": "stream",
84 "cell_type": "markdown",
87 "Then `F` just does that then subtracts the min from the max:\n",
100 "define('AoC2017.2 [maxmin - +] step_zero')"
105 "execution_count": 5,
110 "output_type": "stream",
121 " [2 4 6 8]] AoC2017.2\n",
127 "cell_type": "markdown",
130 "...find the only two numbers in each row where one evenly divides the other - that is, where the result of the division operation is a whole number. They would like you to find those numbers on each line, divide them, and add up each line's result.\n",
132 "For example, given the following spreadsheet:\n",
138 "* In the first row, the only two numbers that evenly divide are 8 and 2; the result of this division is 4.\n",
139 "* In the second row, the two numbers are 9 and 3; the result is 3.\n",
140 "* In the third row, the result is 2.\n",
142 "In this example, the sum of the results would be 4 + 3 + 2 = 9.\n",
144 "What is the sum of each row's result in your puzzle input?"
149 "execution_count": 6,
154 "output_type": "stream",
161 "J('[5 9 2 8] sort reverse')"
166 "execution_count": 7,
171 "output_type": "stream",
173 "[8 5 2] [9 divmod] [8 5 2]\n"
178 "J('[9 8 5 2] uncons [swap [divmod] cons] dupdip')"
182 "cell_type": "markdown",
186 " [9 8 5 2] uncons [swap [divmod] cons F] dupdip G\n",
187 " [8 5 2] [9 divmod] F [8 5 2] G\n",
193 "execution_count": 8,
198 "output_type": "stream",
200 " • [8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip\n",
201 " [8 5 2] • [9 divmod] [uncons swap] dip dup [i not] dip\n",
202 " [8 5 2] [9 divmod] • [uncons swap] dip dup [i not] dip\n",
203 " [8 5 2] [9 divmod] [uncons swap] • dip dup [i not] dip\n",
204 " [8 5 2] • uncons swap [9 divmod] dup [i not] dip\n",
205 " 8 [5 2] • swap [9 divmod] dup [i not] dip\n",
206 " [5 2] 8 • [9 divmod] dup [i not] dip\n",
207 " [5 2] 8 [9 divmod] • dup [i not] dip\n",
208 " [5 2] 8 [9 divmod] [9 divmod] • [i not] dip\n",
209 "[5 2] 8 [9 divmod] [9 divmod] [i not] • dip\n",
210 " [5 2] 8 [9 divmod] • i not [9 divmod]\n",
211 " [5 2] 8 • 9 divmod not [9 divmod]\n",
212 " [5 2] 8 9 • divmod not [9 divmod]\n",
213 " [5 2] 1 1 • not [9 divmod]\n",
214 " [5 2] 1 False • [9 divmod]\n",
215 " [5 2] 1 False [9 divmod] • \n"
220 "V('[8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip')"
224 "cell_type": "markdown",
231 "Given a *sorted* sequence (from highest to lowest) we want to \n",
232 "* for head, tail in sequence\n",
233 " * for term in tail:\n",
234 " * check if the head % term == 0\n",
235 " * if so compute head / term and terminate loop\n",
240 "cell_type": "markdown",
243 "### So we want a `loop` I think\n",
245 " [a b c d] True [Q] loop\n",
246 " [a b c d] Q [Q] loop\n",
248 "`Q` should either leave the result and False, or the `rest` and True.\n",
251 " -----------------\n",
255 " -----------------\n",
260 "cell_type": "markdown",
263 "This suggests that `Q` should start with:\n",
265 " [a b c d] uncons dup roll<\n",
266 " [b c d] [b c d] a\n",
268 "Now we just have to `pop` it if we don't need it.\n",
270 " [b c d] [b c d] a [P] [T] [cons] app2 popdd [E] primrec\n",
271 " [b c d] [b c d] [a P] [a T] [E] primrec\n",
273 "-------------------\n",
275 " w/ Q == [% not] [T] [F] primrec\n",
277 " [a b c d] uncons\n",
279 " [b c d] a [b c d] uncons\n",
280 " [b c d] a b [c d] roll>\n",
281 " [b c d] [c d] a b Q\n",
282 " [b c d] [c d] a b [% not] [T] [F] primrec\n",
284 " [b c d] [c d] a b T\n",
285 " [b c d] [c d] a b / roll> popop 0\n",
287 " [b c d] [c d] a b F Q\n",
288 " [b c d] [c d] a b pop swap uncons ... Q\n",
289 " [b c d] [c d] a swap uncons ... Q\n",
290 " [b c d] a [c d] uncons ... Q\n",
291 " [b c d] a c [d] roll> Q\n",
292 " [b c d] [d] a c Q\n",
294 " Q == [% not] [/ roll> popop 0] [pop swap uncons roll>] primrec\n",
296 " uncons tuck uncons roll> Q"
301 "execution_count": 9,
306 "output_type": "stream",
308 "[8 5 3 2] [9 swap] [9 % not]\n"
313 "J('[8 5 3 2] 9 [swap] [% not] [cons] app2 popdd')"
317 "cell_type": "markdown",
320 "-------------------\n",
322 " [a b c d] uncons\n",
324 " [b c d] a [b c d] [not] [popop 1] [Q] ifte\n",
326 " [b c d] a [] popop 1\n",
329 " [b c d] a [b c d] Q \n",
333 " ---------------\n",
337 " ---------------\n",
341 " w/ Q == [first % not] [first / 0] [rest [not] [popop 1]] [ifte]\n",
345 " a [b c d] [first % not] [first / 0] [rest [not] [popop 1]] [ifte]\n",
346 " a [b c d] first % not\n",
351 " a [b c d] [first % not] [first / 0] [rest [not] [popop 1]] [ifte]\n",
352 " a [b c d] first / 0\n",
356 " a [b c d] [first % not] [first / 0] [rest [not] [popop 1]] [ifte]\n",
357 " a [b c d] rest [not] [popop 1] [Q] ifte\n",
358 " a [c d] [not] [popop 1] [Q] ifte\n",
359 " a [c d] [not] [popop 1] [Q] ifte\n",
361 " a [c d] [not] [popop 1] [Q] ifte\n",
370 " uncons tuck [first % not] [first / 0] [rest [not] [popop 1]] [ifte]\n",
376 "cell_type": "markdown",
379 "### I finally sat down with a piece of paper and blocked it out.\n",
381 "First, I made a function `G` that expects a number and a sequence of candidates and return the result or zero:\n",
384 " ---------------\n",
388 " ---------------\n",
391 "It's a recursive function that conditionally executes the recursive part of its recursive branch\n",
393 " [Pg] [E] [R1 [Pi] [T]] [ifte] genrec\n",
395 "The recursive branch is the else-part of the inner `ifte`:\n",
397 " G == [Pg] [E] [R1 [Pi] [T]] [ifte] genrec\n",
398 " == [Pg] [E] [R1 [Pi] [T] [G] ifte] ifte\n",
400 "But this is in hindsight. Going forward I derived:\n",
402 " G == [first % not]\n",
404 " [rest [not] [popop 0]]\n",
407 "The predicate detects if the `n` can be evenly divided by the `first` item in the list. If so, the then-part returns the result. Otherwise, we have:\n",
409 " n [m ...] rest [not] [popop 0] [G] ifte\n",
410 " n [...] [not] [popop 0] [G] ifte\n",
412 "This `ifte` guards against empty sequences and returns zero in that case, otherwise it executes `G`."
417 "execution_count": 10,
421 "define('G [first % not] [first /] [rest [not] [popop 0]] [ifte] genrec')"
425 "cell_type": "markdown",
428 "Now we need a word that uses `G` on each (head, tail) pair of a sequence until it finds a (non-zero) result. It's going to be designed to work on a stack that has some candidate `n`, a sequence of possible divisors, and a result that is zero to signal to continue (a non-zero value implies that it is the discovered result):\n",
430 " n [...] p find-result\n",
431 " ---------------------------\n",
434 "It applies `G` using `nullary` because if it fails with one candidate it needs the list to get the next one (the list is otherwise consumed by `G`.)\n",
436 " find-result == [0 >] [roll> popop] [roll< popop uncons [G] nullary] tailrec\n",
438 " n [...] p [0 >] [roll> popop] [roll< popop uncons [G] nullary] tailrec\n",
440 "The base-case is trivial, return the (non-zero) result. The recursive branch...\n",
442 " n [...] p roll< popop uncons [G] nullary find-result\n",
443 " [...] p n popop uncons [G] nullary find-result\n",
444 " [...] uncons [G] nullary find-result\n",
445 " m [..] [G] nullary find-result\n",
446 " m [..] p find-result\n",
448 "The puzzle states that the input is well-formed, meaning that we can expect a result before the row sequence empties and so do not need to guard the `uncons`."
453 "execution_count": 11,
457 "define('find-result [0 >] [roll> popop] [roll< popop uncons [G] nullary] tailrec')"
462 "execution_count": 12,
467 "output_type": "stream",
474 "J('[11 9 8 7 3 2] 0 tuck find-result')"
478 "cell_type": "markdown",
481 "In order to get the thing started, we need to `sort` the list in descending order, then prime the `find-result` function with a dummy candidate value and zero (\"continue\") flag."
486 "execution_count": 13,
490 "define('prep-row sort reverse 0 tuck')"
494 "cell_type": "markdown",
497 "Now we can define our program."
502 "execution_count": 14,
506 "define('AoC20017.2.extra [prep-row find-result +] step_zero')"
511 "execution_count": 15,
516 "output_type": "stream",
527 " [3 8 6 5]] AoC20017.2.extra\n",
535 "display_name": "Python 2",
536 "language": "python",
544 "file_extension": ".py",
545 "mimetype": "text/x-python",
547 "nbconvert_exporter": "python",
548 "pygments_lexer": "ipython3",