aboutsummaryrefslogtreecommitdiff
path: root/challenge-336
diff options
context:
space:
mode:
authorRikedyP <rikedyp@gmail.com>2025-08-25 16:40:46 +0100
committerRikedyP <rikedyp@gmail.com>2025-08-25 16:40:46 +0100
commit4bc0d362f907ef7c5933727535bb64f2130b840f (patch)
treeb98966b541d78b19a92452173ad8aa5416d6c7c7 /challenge-336
parentaf21b591aa132d8ffb86b936da361a7d347f99a2 (diff)
downloadperlweeklychallenge-club-4bc0d362f907ef7c5933727535bb64f2130b840f.tar.gz
perlweeklychallenge-club-4bc0d362f907ef7c5933727535bb64f2130b840f.tar.bz2
perlweeklychallenge-club-4bc0d362f907ef7c5933727535bb64f2130b840f.zip
week 336 in APL
Diffstat (limited to 'challenge-336')
-rw-r--r--challenge-336/richard-park/apl/EqualGroup.aplf22
-rw-r--r--challenge-336/richard-park/apl/FinalScore.aplf42
2 files changed, 64 insertions, 0 deletions
diff --git a/challenge-336/richard-park/apl/EqualGroup.aplf b/challenge-336/richard-park/apl/EqualGroup.aplf
new file mode 100644
index 0000000000..84018d781b
--- /dev/null
+++ b/challenge-336/richard-park/apl/EqualGroup.aplf
@@ -0,0 +1,22 @@
+ EqualGroup←{
+ ⍝ Return True (1) if ⍵ can be partitioned into at least one group, all groups of equal size ≥2
+
+ EG←{
+ ⍝ Assumes groups are non-zero, equal or ascending
+ size←⌊/≢¨⊆⍨⍵ ⍝ Compute size of smallest group of consecutive equal values
+ size<2:0 ⍝ Fail is smallest group size is <2
+ grp←⍵⊂⍨⍵≢⍛⍴size↑1 ⍝ Make groups, all size of smallest group
+ ∧/(∧/⊢=⊃)¨grp ⍝ Test groups all contain the same value
+ }
+
+ EG ⍵ ⍝ Run function on input
+
+⍝ Tests
+ Assert←{⍺←'Assertion Failure' ⋄ 0∊⍵:⍺ ⎕SIGNAL 8 ⋄ shy←0}
+ _←Assert 1≡EG 1 1 2 2 2 2
+ _←Assert 0≡EG 1 1 1 2 2 2 3 3
+ _←Assert 1≡EG 5 5 5 5 5 5 7 7 7 7 7 7
+ _←Assert 0≡EG 1 2 3 4
+ _←Assert 1≡EG 8 8 9 9 10 10 11 11
+ 'All tests passed.'
+ }
diff --git a/challenge-336/richard-park/apl/FinalScore.aplf b/challenge-336/richard-park/apl/FinalScore.aplf
new file mode 100644
index 0000000000..52deac4316
--- /dev/null
+++ b/challenge-336/richard-park/apl/FinalScore.aplf
@@ -0,0 +1,42 @@
+ FinalScore←{
+⍝ ⍵: Nested vector of character vectors with scores input
+⍝ ←: Final score
+⍝ Score is computed according to the following rules
+⍝ - scores are processed left to right
+⍝ - add numeric scores to the total
+⍝ - + adds the sum of the previous two scores
+⍝ - C subtracts the previously added score
+⍝ - D doubles the previously added score
+
+ FS←{
+ ⍝ Simple state machine
+ ⍝ Keep score so far + list of previously added scores
+ ⍝ Process one instruction at a time
+ ⍺←0 ⍬ ⋄ (score prev)←⍺
+ 0∊⍴⍵:⍬⍴score
+ next←'¯'@('-'∘=)⊃⍵ ⍝ next instruction
+ ⍝⎕←score(2↑prev)next
+ isnum←1,⍛≡⊃vfi←⎕VFI next
+ n←2⊃vfi
+ isnum:(score+n)(n,prev)∇ 1↓⍵ ⍝ numeric: add value
+ '+',⍛≡next:(score+n)(prev,⍨n←+/2↑prev)∇ 1↓⍵ ⍝ +: add previous two scores
+ 'C',⍛≡next:(score-⊃prev)(1↓prev)∇ 1↓⍵ ⍝ C: subtract previously added value
+ 'D',⍛≡next:(score+n)(prev,⍨n←2×⊃prev)∇ 1↓⍵ ⍝ D: double previously added value
+ }
+
+ FS ⍵
+
+⍝ Tests
+ Assert←{⍺←'Assertion Failure' ⋄ 0∊⍵:⍺ ⎕SIGNAL 8 ⋄ 0}
+
+ _←Assert 30≡FS 0 ⎕JSON'["5","2","C","D","+"]'
+ _←Assert 27≡FS 0 ⎕JSON'["5","-2","4","C","D","9","+","+"]'
+ _←Assert 45≡FS 0 ⎕JSON'["7","D","D","C","+","3"]'
+ _←Assert ¯55≡FS 0 ⎕JSON'["-5","-10","+","D","C","+"]'
+ _←Assert 128≡FS 0 ⎕JSON'["3","6","+","D","C","8","+","D","-2","C","+"]'
+
+ _←Assert 61≡FS,¨'5DDDC3+'
+
+ 'All tests passed.'
+
+ }