8 For each row, determine the difference between the largest value and the
9 smallest value; the checksum is the sum of all of these differences.
11 For example, given the following spreadsheet:
19 - The first row's largest and smallest values are 9 and 1, and their
21 - The second row's largest and smallest values are 7 and 3, and their
23 - The third row's difference is 6.
25 In this example, the spreadsheet's checksum would be 8 + 4 + 6 = 18.
29 from notebook_preamble import J, V, define
31 I'll assume the input is a Joy sequence of sequences of integers.
39 So, obviously, the initial form will be a ``step`` function:
43 AoC2017.2 == 0 swap [F +] step
45 This function ``F`` must get the ``max`` and ``min`` of a row of numbers
46 and subtract. We can define a helper function ``maxmin`` which does
51 define('maxmin == [max] [min] cleave')
63 Then ``F`` just does that then subtracts the min from the max:
73 define('AoC2017.2 == [maxmin - +] step_zero')
91 ...find the only two numbers in each row where one evenly divides the
92 other - that is, where the result of the division operation is a whole
93 number. They would like you to find those numbers on each line, divide
94 them, and add up each line's result.
96 For example, given the following spreadsheet:
104 - In the first row, the only two numbers that evenly divide are 8 and
105 2; the result of this division is 4.
106 - In the second row, the two numbers are 9 and 3; the result is 3.
107 - In the third row, the result is 2.
109 In this example, the sum of the results would be 4 + 3 + 2 = 9.
111 What is the sum of each row's result in your puzzle input?
115 J('[5 9 2 8] sort reverse')
125 J('[9 8 5 2] uncons [swap [divmod] cons] dupdip')
130 [8 5 2] [9 divmod] [8 5 2]
135 [9 8 5 2] uncons [swap [divmod] cons F] dupdip G
136 [8 5 2] [9 divmod] F [8 5 2] G
140 V('[8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip')
145 . [8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip
146 [8 5 2] . [9 divmod] [uncons swap] dip dup [i not] dip
147 [8 5 2] [9 divmod] . [uncons swap] dip dup [i not] dip
148 [8 5 2] [9 divmod] [uncons swap] . dip dup [i not] dip
149 [8 5 2] . uncons swap [9 divmod] dup [i not] dip
150 8 [5 2] . swap [9 divmod] dup [i not] dip
151 [5 2] 8 . [9 divmod] dup [i not] dip
152 [5 2] 8 [9 divmod] . dup [i not] dip
153 [5 2] 8 [9 divmod] [9 divmod] . [i not] dip
154 [5 2] 8 [9 divmod] [9 divmod] [i not] . dip
155 [5 2] 8 [9 divmod] . i not [9 divmod]
156 [5 2] 8 . 9 divmod not [9 divmod]
157 [5 2] 8 9 . divmod not [9 divmod]
158 [5 2] 1 1 . not [9 divmod]
159 [5 2] 1 False . [9 divmod]
160 [5 2] 1 False [9 divmod] .
168 Given a *sorted* sequence (from highest to lowest) we want to \* for
169 head, tail in sequence \* for term in tail: \* check if the head % term
170 == 0 \* if so compute head / term and terminate loop \* else continue
172 So we want a ``loop`` I think
173 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
177 [a b c d] True [Q] loop
180 ``Q`` should either leave the result and False, or the ``rest`` and
193 This suggests that ``Q`` should start with:
197 [a b c d] uncons dup roll<
200 Now we just have to ``pop`` it if we don't need it.
204 [b c d] [b c d] a [P] [T] [cons] app2 popdd [E] primrec
205 [b c d] [b c d] [a P] [a T] [E] primrec
211 w/ Q == [% not] [T] [F] primrec
215 [b c d] a [b c d] uncons
216 [b c d] a b [c d] roll>
218 [b c d] [c d] a b [% not] [T] [F] primrec
221 [b c d] [c d] a b / roll> popop 0
223 [b c d] [c d] a b F Q
224 [b c d] [c d] a b pop swap uncons ... Q
225 [b c d] [c d] a swap uncons ... Q
226 [b c d] a [c d] uncons ... Q
227 [b c d] a c [d] roll> Q
230 Q == [% not] [/ roll> popop 0] [pop swap uncons roll>] primrec
232 uncons tuck uncons roll> Q
236 J('[8 5 3 2] 9 [swap] [% not] [cons] app2 popdd')
241 [8 5 3 2] [9 swap] [9 % not]
250 [b c d] a [b c d] [not] [popop 1] [Q] ifte
267 w/ Q == [first % not] [first / 0] [rest [not] [popop 1]] [ifte]
271 a [b c d] [first % not] [first / 0] [rest [not] [popop 1]] [ifte]
272 a [b c d] first % not
277 a [b c d] [first % not] [first / 0] [rest [not] [popop 1]] [ifte]
282 a [b c d] [first % not] [first / 0] [rest [not] [popop 1]] [ifte]
283 a [b c d] rest [not] [popop 1] [Q] ifte
284 a [c d] [not] [popop 1] [Q] ifte
285 a [c d] [not] [popop 1] [Q] ifte
287 a [c d] [not] [popop 1] [Q] ifte
296 uncons tuck [first % not] [first / 0] [rest [not] [popop 1]] [ifte]
298 I finally sat down with a piece of paper and blocked it out.
299 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
301 First, I made a function ``G`` that expects a number and a sequence of
302 candidates and return the result or zero:
314 It's a recursive function that conditionally executes the recursive part
315 of its recursive branch
319 [Pg] [E] [R1 [Pi] [T]] [ifte] genrec
321 The recursive branch is the else-part of the inner ``ifte``:
325 G == [Pg] [E] [R1 [Pi] [T]] [ifte] genrec
326 == [Pg] [E] [R1 [Pi] [T] [G] ifte] ifte
328 But this is in hindsight. Going forward I derived:
334 [rest [not] [popop 0]]
337 The predicate detects if the ``n`` can be evenly divided by the
338 ``first`` item in the list. If so, the then-part returns the result.
343 n [m ...] rest [not] [popop 0] [G] ifte
344 n [...] [not] [popop 0] [G] ifte
346 This ``ifte`` guards against empty sequences and returns zero in that
347 case, otherwise it executes ``G``.
351 define('G == [first % not] [first /] [rest [not] [popop 0]] [ifte] genrec')
353 Now we need a word that uses ``G`` on each (head, tail) pair of a
354 sequence until it finds a (non-zero) result. It's going to be designed
355 to work on a stack that has some candidate ``n``, a sequence of possible
356 divisors, and a result that is zero to signal to continue (a non-zero
357 value implies that it is the discovered result):
361 n [...] p find-result
362 ---------------------------
365 It applies ``G`` using ``nullary`` because if it fails with one
366 candidate it needs the list to get the next one (the list is otherwise
371 find-result == [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec
373 n [...] p [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec
375 The base-case is trivial, return the (non-zero) result. The recursive
380 n [...] p roll< popop uncons [G] nullary find-result
381 [...] p n popop uncons [G] nullary find-result
382 [...] uncons [G] nullary find-result
383 m [..] [G] nullary find-result
386 The puzzle states that the input is well-formed, meaning that we can
387 expect a result before the row sequence empties and so do not need to
388 guard the ``uncons``.
392 define('find-result == [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec')
396 J('[11 9 8 7 3 2] 0 tuck find-result')
404 In order to get the thing started, we need to ``sort`` the list in
405 descending order, then prime the ``find-result`` function with a dummy
406 candidate value and zero ("continue") flag.
410 define('prep-row == sort reverse 0 tuck')
412 Now we can define our program.
416 define('AoC20017.2.extra == [prep-row find-result +] step_zero')
424 [3 8 6 5]] AoC20017.2.extra