From dc249b163b0a950e64ef5788502b92b7024461a0 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Sun, 22 Aug 2021 12:04:12 +0100 Subject: - Added APL solutions by Richard Park. --- challenge-126/richard-park/apl/ch-1.aplf | 19 +++++++++++++++++++ challenge-126/richard-park/apl/ch-2.aplf | 11 +++++++++++ 2 files changed, 30 insertions(+) create mode 100644 challenge-126/richard-park/apl/ch-1.aplf create mode 100644 challenge-126/richard-park/apl/ch-2.aplf diff --git a/challenge-126/richard-park/apl/ch-1.aplf b/challenge-126/richard-park/apl/ch-1.aplf new file mode 100644 index 0000000000..fbb3ac764b --- /dev/null +++ b/challenge-126/richard-park/apl/ch-1.aplf @@ -0,0 +1,19 @@ + CountNumbers←{ + (⎕IO ⎕ML)←1 + 10>⍵:⍵-1 ⍝ Less than 10, n-1 + m←10*⌊10⍟⍵ ⍝ Magnitude (m) + k←⌊⍵÷m ⋄ r←¯1+k ⍝ Most significant digit (k) // Recursion factor (r) + 2>k:∇ ¯1+m ⍝ No new numbers start with 1, e.g. (1000 to 1999) + (∇ 1⌈m|⍵)+r+r×∇ ¯1+m ⍝ Recurse (∇) lesser significant digits and lower magnitudes + +⍝ A brute force version: +⍝ Converts integers up to ⍵ into character vectors and removes those that contain '1' +⍝ CountNumbersBrute ← {+/~'1'∘∊¨⍕¨⍳⍵} + +⍝ Memoisation: +⍝ Performance can be significantly improved with dfns.memo +⍝ 'memo'⎕CY'dfns' +⍝ 'cache'⎕NS⍬ +⍝ cache.(keys vals)←⍬⍬ +⍝ CountNumbersMemo ← CountNumbers memo cache + } diff --git a/challenge-126/richard-park/apl/ch-2.aplf b/challenge-126/richard-park/apl/ch-2.aplf new file mode 100644 index 0000000000..deafded6b2 --- /dev/null +++ b/challenge-126/richard-park/apl/ch-2.aplf @@ -0,0 +1,11 @@ + Minesweeper←{ + (⎕IO ⎕ML)←1 +⍝ Example input: 5 10⍴'× × ×××× × × × × ×× × × ×' + CellCount←{ + ⍝ A cell is a 3 by 3 area + c←'×'=,⍵ ⍝ 1 is a mine, 0 is not + 5⊃c:'×' ⍝ Middle is a mine? + +/c ⍝ Else count mines in cell + } + CellCount⌺3 3⊢⍵ + } -- cgit