-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday2.apl
More file actions
32 lines (25 loc) · 2.05 KB
/
day2.apl
File metadata and controls
32 lines (25 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Input ← ⎕FIO[26] 'input/day2.txt' ⍝ Read entire file
Data ← 16 16 ⍴ ⍎ Input ⍝ Convert to vector by executing (⍎) and reshape
⍝ Solution to part A
a ← {+/(⌈/⍵)-⌊/⍵}
⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝
⍝ ⌊/⍵ ⍝ Take minimum of each matrix ⍵ row
⍝ ⌈/⍵ ⍝ Take maximum of each matrix ⍵ row
⍝ - ⍝ Substract minimum from maximum
⍝ +/ ⍝ Reduce by summing
⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝
a Data
⍝ Solution to part B
b ← {+/{1~⍨⍵/⍨⍵=⌊⍵}¨{,⍵∘.÷⍵}¨,/⍵}
⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝
⍝ ,/⍵ ⍝ Convert matrix to nested arrays
⍝ { }¨ ⍝ Apply lambda to each element
⍝ ⍵∘.÷⍵ ⍝ Vector outer product
⍝ , ⍝ Flatten result matrix to a vector
⍝ { }¨ ⍝ Apply lambda to each element
⍝ ⍵=⌊⍵ ⍝ Find whole number index
⍝ ⍵/¨ ⍝ Select whole numbers from ⍵
⍝ 1~⍨ ⍝ Remove 1s
⍝ +/ ⍝ Reduce by summing
⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝
b Data