aboutsummaryrefslogtreecommitdiff
path: root/challenge-064/richard-park/apl/MinimumSumPath.aplf
blob: 2256772e1e38a6c8fdacb1b266190575006a79ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 MinimumSumPath{
⍝ Going from top-left to bottom-right in matrix ⍵
⍝ travelling in single steps either right or down only
⍝ find the path which has the minimum sum of values for such a path

⍝ ⍵: Numeric matrix
⍝ ←: (sum)(path_indices)(path_values)
     ⎕IO1
     dpl¯1+⍴         ⍝ Directional Path Length: Max steps in each direction
     pl+/dpl          ⍝ Path Length: Length of valid paths
     mpn2⊥⌽0 1/⍨dpl   ⍝ Max Path Number: Max binary number representing a valid path
     s(1 0)(0 1)                                 ⍝ Steps (down)(right)
     paths{⌿⍨(⊃⌽dpl)=+/}(2¯1)(pl÷2)+⍳mpn   ⍝ Valid paths (0 down, 1 right)
     pi1 1+1+⍀⍤2s[1+paths;]                     ⍝ Valid path indices
     pv(),[pi]                                ⍝ Valid path values
     sum/+/pv                                    ⍝ Minimum Sum
     mspi(⊢⍳⌊/)+/pv                               ⍝ Minimum Sum Path Index
     path_indices1 1mspipi                      ⍝ Minimum Sum Path Indices
     path_valuesmspipv                           ⍝ Minimum Sum Path Values
     (sum)(path_indices)(path_values)
 }