-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday1.apl
More file actions
19 lines (15 loc) · 1.38 KB
/
day1.apl
File metadata and controls
19 lines (15 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
⎕IO ← 0 ⍝ Set indexing start to zero
Input ← ⎕FIO[26] 'input/day1.txt' ⍝ Read entire file as byte vector
Data ← '0123456789' ⍳ Input ⍝ Convert to digit array (needs IO ← 0 to work)
f ← {+/⍵/⍨⍵=⍺⌽⍵} ⍝ Solve the puzzle
⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝
⍝ ⍺⌽⍵ ⍝ Rotate right argument (⍵) by left argument (⍺) places
⍝ ⍵= ⍝ Find places where rotated vector matches original
⍝ ⍵/ ⍝ Select only elements from ⍵ where above expression is true
⍝ ⍨ ⍝ Swap arguments to / operator to filter ⍵ by ⍵=⍺⌽⍵.
⍝ Otherwise the program would look {+/(⍵=⍺⌽⍵)/⍵}
⍝ Not sure if this trick is usefull apart code golf
⍝ +/ ⍝ Reduce vector by summing its elements
⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝
1 f Data ⍝ Part A
((⍴ Data) ÷ 2) f Data ⍝ Part B