/ APL / 2021 / 14.apl
14.apl
1  input ← ⊃⎕NGET '../.cache/2021/14' 1
2  ⎕PP ← 32
3  template ← ⊃input
4  rules ← {(⍵⌷⍨⊢)¨1 2 7}¨2↓input
5  gr ← {k←⍵⋄{⊃⌽⊃⍵/⍨{k≡2↑⍵}¨⍵}rules}
6  next ← {{w←⍵⋄∪{k←⍵⋄⍵ (+/{(2⊃⍵)×k≡⊃⍵}¨w)}¨{⊃⍵}¨w}⊃,/{(a b) c←⍵⋄x←gr ⊃⍵⋄((a x)c)((x b)c)}¨⍵}
7  solve ← {{(⌈/⍵)-(⌊/⍵)}⊃⌽↓⍉{a b←⍵⋄({⊃⌽⍵}¨a) {⍺,+/⍵}⌸ b}↓⍉↑ (⊂((1↑template) 1)),(next⍣⍵) ↓{(⊃⍺)(≢⍵)}⌸2,/template}
8  ⎕ ← solve 10
9  ⎕ ← solve 40