-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay17.apln
42 lines (35 loc) · 1.23 KB
/
Day17.apln
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
33
34
35
36
37
38
39
40
41
42
:Namespace Day17
Foldl←{⍺ ⍺⍺ #.U.Foldl ⍵}
Silver←{ ⍝ ⍺←target sum, ⍵←item sizes
⍝ For each item, compute array of # of possible combinations,
⍝ so that array[i+⎕IO] is # of combinations with sum i.
(1+⍺)⊃⊃1 {⍺(,+,⍨)⍵⍴0}Foldl ⍵
}
Gold←{ ⍝ ⍺←target sum, ⍵←item sizes
⍝ Do the same for Silver, but accumulate a matrix instead,
⍝ so that array[r+⎕IO;i+⎕IO] is # of combinations using r items with sum i.
⊃0~⍨(1+⍺)⊃⍤1⊃⊃(⊂1 1⍴1) {⊂(⊃⍺)(↑⍨+↑⍨∘-)1⍵+⍴⊃⍺}Foldl ⍵
}
ExampleData←20 15 10 5 5
SilverTest←{
f←⍺⍺
Assert←#.U.Assert
Assert 4≡25 f ExampleData:
'All tests passed'
}
GoldTest←{
f←⍺⍺
Assert←#.U.Assert
Assert 3≡25 f ExampleData:
'All tests passed'
}
∇ r←Data
r←⍎¨#.U.ReadLines'input/2015/17.txt'
∇
∇ TestAll
' Silver Test: ',Silver SilverTest 0
' Gold Test: ',Gold GoldTest 0
'Silver Solution: ',⍕150 Silver Data
' Gold Solution: ',⍕150 Gold Data
∇
:EndNamespace