adventofcode

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

puzzle_10.apl (766B)


      1 #! /usr/bin/apl --script
      2 
      3 input← ⎕FIO[49] 'files/10.txt'
      4 
      5 p← {(2|⍳2×⍴⍵ )⊂ 2/⍵ }¨input ⍝ partition chars
      6 s← +⌿+/(3×p=')') (¯3×p='(') (57×p=']') (¯57×p='[') (1197×p='}') (¯1197×p='{') (25137×p='>') (¯25137×p='<') ⍝ map scores
      7 f← {(2⌷⍴ b)⍴ b← ⍉ ⊃ ⍵ } ¨ ⊃ s ⍝ formatting
      8 
      9 r← {⍵ /⍨0≠(w,1)×1,w← 2{(~q)+(⍺+⍵ )×q← ⍺<⍵ }/⍵ } ⍝ reduce connected chunks
     10 m← {(r ⍣ {(⍴ ⍺) = ⍴ ⍵ }) ⍵ } ¨ f ⍝ do r until all chunks reduced
     11 
     12 +/{↑ (0 >⍨ ⍵ ) / ⍵ } ¨ m ⍝ part 1
     13 
     14 i← ⌽ ¨m/⍨ 0=+/⊃ 0>⍨m ⍝ find all lines with incomplete chunks
     15 y[⍋ y] ⌷⍨ ⌈2÷⍨⍴ y← {{⍺+5×⍵ }/0,⍨⍵ }¨⌽ ¨(1ׯ3=i)+(2ׯ57=i)+(3ׯ1197=i)+(4ׯ25137=i) ⍝ part 2
     16 
     17 )OFF