adventofcode

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

puzzle_4.apl (812B)


      1 #! /usr/bin/apl --script
      2 
      3 input← ⎕FIO[49] 'files/4.txt'
      4 
      5 numbers← 1 + ⍎↑ input
      6 table_rows← 1 + ⍎ ¨ ((⍴ s) ⍴ ⍉ ∊ { 0 < ⍴ ⍵ } ¨ s) / s← 1↓ input
      7 t← {⊃ table_rows ⌷⍨⊂ (⍵ × 5) + ⍳5} ¨ 1 -⍨ ⍳5 ÷⍨ ⍴ table_rows ⍝ table matrices
      8 
      9 p← { { (1++/+⌿⍵ - 0 >⍨ ⍵ ) × {(0 ∊ +⌿⊃ ⍵ )∨ 0 ∊ +/⊃ ⍵ } ⍵ } ¨ ⍵ } ⍝ calculate winners
     10 b← { ⊃ { ⍵ × ⍺ ≠ ⍵ }/(⌽ ⍵ ↑ numbers),⊂ t } ⍝ calculate reduced bingo round
     11 w← { ⍵ /1 i⍴ ⍳i← ≢ ⍵ } ⍝ where (⍸)
     12 
     13 all← {p b ⍵ } ¨ ⍳⍴ numbers
     14 
     15 (1 -⍨ j⊃ a) × (numbers ⊃ ⍨ j← ↑ w a← +/¨all) - 1 ⍝ part 1
     16 (1 -⍨ numbers ⊃ ⍨ s) × (((1/⍨⍴ t) - 0 >⍨ p b s - 1) / (s← ↑ ⊃ w (⍴ t) = g← +/ ¨ 0 >⍨ all) ⊃ all) - 1 ⍝ part 2
     17 
     18 )OFF