Skip to content

Instantly share code, notes, and snippets.

@rodrigogiraoserrao
Last active August 6, 2020 15:26
Show Gist options
  • Save rodrigogiraoserrao/393a63a1af1b21c7edbd98491d5b3c70 to your computer and use it in GitHub Desktop.
Save rodrigogiraoserrao/393a63a1af1b21c7edbd98491d5b3c70 to your computer and use it in GitHub Desktop.
RGS's solution for Problem 2, Phase 2 of 2020 APL competition (see https://mathspp.com/blog/2020-apl-competition for my thoughts on it)
Steps ← {
(⎕IO ⎕ML ⎕WX) ← 0 1 3
⍝ Utility function to generate ranges of numbers.
⍝ Dyadic function expecting a number on the left (mode) and a 2-element number vector (s f) on the right (steps start and end points).
⍝ - if ⍺ is negative, return vector with |⌊⍺ equally-sized steps from s to f.
⍝ - if ⍺ is positive, ⍺ represents the step size.
⍝ - if ⍺ is 0, return s
⍝ Returns a vector of numbers between s and f.
⍺ ← 1
(s f) ← ⍵
r ← f-s
⍝⍺<0:⍵ ∇⍨(f-s)÷|⌊⍺ ⍝ clever but fails the ¯4 Steps 42 42 test case
⍺<0: s + (⎕IO-⍨⍳st+1) × (r) ÷ st←|⌊⍺
⍺=0: s
⍺>0: f ,⍨ s+(×r)×⍺×⎕IO -⍨ ⍳⌈|r÷⍺
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment