15 KiB
15 KiB
In [1]:
from notebook_preamble import J, V, defineIn [2]:
define('maxmin == [max] [min] cleave')In [3]:
J('[1 2 3] maxmin')3 1
In [4]:
define('AoC2017.2 == [maxmin - +] step_zero')In [5]:
J('''
[[5 1 9 5]
[7 5 3]
[2 4 6 8]] AoC2017.2
''')18
In [6]:
J('[5 9 2 8] sort reverse')[9 8 5 2]
In [7]:
J('[9 8 5 2] uncons [swap [divmod] cons] dupdip')[8 5 2] [9 divmod] [8 5 2]
In [8]:
V('[8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip') . [8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip
[8 5 2] . [9 divmod] [uncons swap] dip dup [i not] dip
[8 5 2] [9 divmod] . [uncons swap] dip dup [i not] dip
[8 5 2] [9 divmod] [uncons swap] . dip dup [i not] dip
[8 5 2] . uncons swap [9 divmod] dup [i not] dip
8 [5 2] . swap [9 divmod] dup [i not] dip
[5 2] 8 . [9 divmod] dup [i not] dip
[5 2] 8 [9 divmod] . dup [i not] dip
[5 2] 8 [9 divmod] [9 divmod] . [i not] dip
[5 2] 8 [9 divmod] [9 divmod] [i not] . dip
[5 2] 8 [9 divmod] . i not [9 divmod]
[5 2] 8 . 9 divmod not [9 divmod]
[5 2] 8 9 . divmod not [9 divmod]
[5 2] 1 1 . not [9 divmod]
[5 2] 1 False . [9 divmod]
[5 2] 1 False [9 divmod] .
In [9]:
J('[8 5 3 2] 9 [swap] [% not] [cons] app2 popdd')[8 5 3 2] [9 swap] [9 % not]
In [10]:
define('G == [first % not] [first /] [rest [not] [popop 0]] [ifte] genrec')In [11]:
define('find-result == [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec')In [14]:
J('[11 9 8 7 3 2] 0 tuck find-result')3.0
In [12]:
define('prep-row == sort reverse 0 tuck')In [13]:
define('AoC20017.2.extra == [prep-row find-result +] step_zero')In [15]:
J('''
[[5 9 2 8]
[9 4 7 3]
[3 8 6 5]] AoC20017.2.extra
''')9.0