adventofcode

https://adventofcode.com/
Log | Files | Refs

puzzle_14.apl (654B)


      1 #! /usr/bin/apl --script
      2 
      3 input← ⎕FIO[49] 'files/14.txt'
      4 
      5 pad← {⊂(1 -⍨ 2 × ⍴ ⍵) ⍴ ∊⍵,¨' '/⍨⍴⍵} ⍝ insert space between every char in string
      6 rules← {((↑↑⍵),' ',↑⌽↑⍵) (↑⌽⍵)} ¨ {⍵⊂⍨~⍵∊'-> '} ¨ 2 ↓ input ⍝ formatted rules
      7 insert← {∊ a ({(k↑y),,/(k←3-⍨⍴y)↓y←1⌽(⊂1⍴⍺),⍨¯1⌽⍵} ⍣ d) (1↓(d←(' '≠a←↑⌽⍺)×+/⍵ ⍷⍨ ↑⍺),⍵⊂⍨~¯1⌽⍵ ⍷⍨ ↑⍺)} ⍝ apply rule
      8 
      9 q←{+/+⌿⍵} ¨ ⎕AV[65+⍳26] ∘.= ({insert / rules,pad ⊃ ⍵} ⍣ 10) 1 ⌷ input ⍝ find quantities
     10 (↑f) - ↑⌽f← o /⍨ 0 ≠ o←q[⍒ q] ⍝ part 1
     11 
     12 )OFF