Skip to content

Instantly share code, notes, and snippets.

@GrayJoKing
Created August 5, 2020 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GrayJoKing/d14fe5cda04b191d0fd33d7239911849 to your computer and use it in GitHub Desktop.
Save GrayJoKing/d14fe5cda04b191d0fd33d7239911849 to your computer and use it in GitHub Desktop.
JoKing's answers for Phase 1 and 2
:Namespace Phase1
P01←(⊂∘⍋0,⊣)⌷(⊂↑),(⊂↓) ⍝ (⊂∘⍋0,⊣)⌷↑,⍥⊂↓
P02←(<∘128∨>∘191)⊂⊢
P03←⎕A∘⍳⊥⍨26/⍨≢
P04←{~≥/⌽×4 25 4⊤⍵}¨
P05←⊃+{⎕IO-⍳1+|-/⍵}×(×-/)
P06←(⊂∘⍋≠)⌷⊢
P07←{∧/⊃∊/ (⍸∘⌽ 2∘⊥⍣¯1)¨ ⍺⍵} ⍝ ∧/∊⍥(⍸∘⌽ 2∘⊥⍣¯1)
P08←∧/0>2×/2-/10∘⊥⍣¯1
P09←(⍳∘≢≡⍋) (⌽+⊃-⊃∘⌽)@(∨\⊢=⌈/)∘,
P10←{1↓⊃,/ ⍵} (,(⎕UCS 13),⍕)¨
:EndNamespace
:Namespace Phase2
(⎕IO ⎕ML ⎕WX)←1 1 3
⍝ 2020 APL Problem Solving Competition Phase II
⍝ Stub function for Problem 1, Task 1 - DiveScore
⍝ Put your code and comments below here
DiveScore ← ⍎2⍕ +.×∘ (3↑(2÷⍨3-⍨≢)↓ ⊂∘⍋⌷⊢)
⍝ 2020 APL Problem Solving Competition Phase II
⍝ Stub function for Problem 2, Task 1 - Steps
⍝ Put your code and comments below here
Steps ← (⊃⊢)-{⍺←1 ⋄ (×⍵) × (|⍵) ⌊ ⊃{+\ 0,⍵ /⍨ ⌈|⍺}/ (⍺,|⍵÷1⌈|⍺-(⍺<0)×1|⍺)[⍒0⍺]}∘(-/)
⍝ 2020 APL Problem Solving Competition Phase II
⍝ Stub function for Problem 3, Task 1 - PastTasks
⍝ Put your code and comments below here
PastTasks ← (('base href="(.*)"' ⎕S '\1') ,¨ ('href="([^"]*\.pdf)"' ⎕S '\1')) {(HttpCommand.Get ⍵).Data}
⍝ 2020 APL Problem Solving Competition Phase II
⍝ Stub function for Problem 4, Task 1 - revp
⍝ Put your code and comments below here
DNAindexes ← {⎕IO}-⍨'ACGT'∘⍳
range4_12 ← 4↓{⎕IO}-⍨∘⍳13⌊≢
ReversePalindromes ← (⍸∧/¨)3=⊢+⌽¨
revp ← (↑∘↑,/) range4_12 (⊣ ,¨⍨∘ReversePalindromes ,/)¨ ⊂∘DNAindexes
⍝ 2020 APL Problem Solving Competition Phase II
⍝ Stub function for Problem 4, Task 2 - sset
⍝ Put your code and comments below here
sset ← {(10*6) (|∘(2∘×)⍣⍵) 1}
⍝ 2020 APL Problem Solving Competition Phase II
⍝ Stub function for Problem 5, Task 1 - rr
⍝ Put your code and comments below here
rr ← ⊣+ 0, {⌽ +⌿ ↑ (¯1↓⍺) × ⌽ (⌽×\∘⌽)¨ ,\ ⌽ 1+1↓⍵}
⍝ 2020 APL Problem Solving Competition Phase II
⍝ Stub function for Problem 5, Task 2 - pv
⍝ Put your code and comments below here
pv ← ⊣+.÷(×\1+⊢)
⍝ 2020 APL Problem Solving Competition Phase II
⍝ Stub function for Problem 6, Task 1 - Merge
⍝ Put your code and comments below here
Merge ← {
n ← ⎕JSON ⊃ ⎕NGET ⍵ ⍝ Store the values of the JSON in namespace n
jsonVal ← { ⍝ Function handling each value
(⊂⍵) ∊ n.⎕NL ¯2 : ⍕n⍎⍵ ⍝ If it is in the JSON, return the corresponding value
'' ≢ ⍵ : '???' ⋄ '@' ⍝ If it is empty, return @ otherwise ???
}
'@[^@]*?@' ⎕R (jsonVal ¯1↓1↓ {⍵.Match}) ⊃ ⎕NGET ⍺ ⍝ And apply this replace regex to the left argument
}
⍝ 2020 APL Problem Solving Competition Phase II
⍝ Stub function for Problem 7, Task 1 - CheckDigit
⍝ Put your code and comments below here
CheckDigit ← {10|-⍵+.×11⍴3 1}
⍝ 2020 APL Problem Solving Competition Phase II
⍝ Stub function for Problem 7, Task 2 - WriteUPC
⍝ Put your code and comments below here
digitCodes ← ⍎¨¨ '0001101' '0011001' '0010011' '0111101' '0100011' '0110001' '0101111' '0111011' '0110111' '0001011'
WriteUPC ← {
∨/ (⍵ > 9),(⍵ < 0),(11 ≠ ≢⍵) : ¯1
∊ 1 0 1 (digitCodes[⎕IO + 6↑⍵]) 0 1 0 1 0 (~digitCodes[⎕IO + 6↓⍵,CheckDigit ⍵]) 1 0 1
}
⍝ 2020 APL Problem Solving Competition Phase II
⍝ Stub function for Problem 7, Task 3 - ReadUPC
⍝ Put your code and comments below here
ReadUPC ← {
(11 ⍴ 1 0) ≢ 3⌽42↓5⌽42↓3⌽⍵ : ¯1 ⍝ Determine if a barcode is in a valid format
allDigits ← ¯42⌽5↓42⌽¯3↓3↓⍵ ⍝ Remove the excess digits from the barcode
digitSections ← (⌽⍣(~digitCodes ∊⍨ ⊂7↑allDigits)) allDigits ⍝ Flip the sequence if the first section is not valid
digitSections ⍴⍨← 12 7 ⍝ Split the digits into sections of 7
digits ← ⎕IO -⍨ digitCodes ⍳ ↓ (6/0 1) ≠[1] digitSections ⍝ Invert the second half of the digitSections and index into the digitCodes
∨/ (10|digits+.×12⍴3 1), 9<digits : ¯1 ⍝ If the check digit is wrong or any of the digitSections are invalid, return ¯1
digits ⍝ Otherwise just return the digits themselves
}
⍝ 2020 APL Problem Solving Competition Phase II
⍝ Stub function for Problem 8, Task 1 - Balance
⍝ Put your code and comments below here
Balance ← {
⍝ Helper function to check if a subset of the right argument adds up to the left argument
⍝ Returning ¯1 if it doesn't, else a boolean mask of the appropriate subset
addsUpTo ← {
⍺ = 0 : 0 /⍨ ≢⍵ ⍝ If ⍺ is zero, then we've done it, hooray! Return zero for the rest of the elements, we won't need them
⍵ ≡ ⍬ : ¯1 ⍝ If we're out of elements and the total still isn't zero, return ¯1
r ← (⍺-⊃⍵) ∇ 1↓⍵ ⍝ Try including this element
r ≢ ¯1 : 1,r ⍝ If this is a sucessful, prepend a 1
r ← ⍺ ∇ 1↓⍵ ⍝ Otherwise try not including this element
r ≢ ¯1 : 0,r ⍝ Was this successful? If so, prepend a zero
¯1 ⍝ If not, return ¯1
}
2| +/ ⍵ : ⍬ ⍝ If the total is odd return empty subset
sorted ← ⍵[⍒⍵] ⍝ Sort the right argument
mask ← ((⊃sorted)-⍨2÷⍨+/⍵) addsUpTo 1↓sorted ⍝ Find a subset that adds up to half the total
mask ≡ ¯1 : ⍬ ⍝ If that doesn't exist, return an empty subset
mask ,⍨← 1 ⍝ To be slightly more efficient, we know that the first element has to be in one of the subsets
(sorted[⍸mask]) (sorted[⍸~mask]) ⍝ Split the list into two subsets
}
⍝ 2020 APL Problem Solving Competition Phase II
⍝ Stub function for Problem 9, Task 1 - Weights
⍝ Put your code and comments below here
Weights ← {
⍝ Helper function to parse a mobile into a recursive array
parseMobile ← {
(mobileRoot ← ⍺⌷⊃⍵) ∊ ⎕A : mobileRoot ⍝ If the current mobile is just a letter, return it
mobileRoot = '│' : ⍺ ∇ 1↓⍵ ⍝ If it is a | the remove this line and continue at the same index on the next line
mobiles ← (('┌─┴┐' ∊⍨ ⊃⍵) ⊆ ⍳≢⊃⍵) ⍝ Get the list of the indexes of the mobiles in the form ┌──┴──┐ on this line
current ← ∊ mobiles[⍺⍳⍨⍸'┴'=⊃⍵] ⍝ Find the mobile which contains the current root
ratios ← (⍺-⍨⊃⌽current) (,÷∨) (⍺-⊃current) ⍝ Get the ratio of the left arm to the right using gcd
mobileStructure ← (⊂∇∘(1↓⍵))¨ ⌽2↑¯1⌽current ⍝ Recursively run the function over the ends of the mobile
ratios ,¨ mobileStructure ⍝ Return the structures paired up with their ratios
}
⍝ This takes the remaining lines of the mobile as its right argument and the current index of the root of the mobile it is parsing as the left
⍝ The structure of the returned array is
⍝┌──────────────────────┬──────────────────────┐
⍝│┌─────────┬──────────┐│┌─────────┬──────────┐│
⍝││ratio of │details of│││ratio of │details of││
⍝││left arm │ left arm │││right arm│right arm ││
⍝│└─────────┴──────────┘│└─────────┴──────────┘│
⍝└──────────────────────┴──────────────────────┘
⍝ Where the details of each arm are of the same structure, unless the arm terminates, in which case it is just the letter itself.
⍝ For example, the second test case ends up in the form
⍝┌─────────────┬───────────────────────┐
⍝│┌─┬─────────┐│┌─┬───────────────────┐│
⍝││2│┌───┬───┐│││1│┌─────────────┬───┐││
⍝││ ││2 A│1 B││││ ││┌─┬─────────┐│1 C│││
⍝││ │└───┴───┘│││ │││1│┌───┬───┐││ │││
⍝│└─┴─────────┘││ │││ ││1 D│1 E│││ │││
⍝│ ││ │││ │└───┴───┘││ │││
⍝│ ││ ││└─┴─────────┘│ │││
⍝│ ││ │└─────────────┴───┘││
⍝│ │└─┴───────────────────┘│
⍝└─────────────┴───────────────────────┘
⍝ Helper function that finds the minimum weight of a mobile
multReduce ← {
1 = ≢⍵ : 1 ⍝ If we're at a base case, return 1
(+/ ⊃¨ ⍵) × ∧/ (∇ ⊃∘⌽)¨ ⍵ ⍝ Otherwise multiply the current mobile's ratio sum by the lcm of the arms
}
⍝ Helper function that takes a recursive mobile and its total weight and returns a array of pairs of letters to weights
calcTotals ← {
1 = ≢⍵ : ⊂⍵ ⍺ ⍝ If we're at the base case, this letter has the current weight
split ← (⊢ × ⍺ ÷ +/) ⊃¨ ⍵ ⍝ Split the weight by the ratio
values ← split ∇¨ ⊃∘⌽¨ ⍵ ⍝ Recurse with the weight
(⊃ , ⊃∘⌽) values ⍝ And join the results
}
lines ← (⊢ ⊆⍨ (⎕UCS 10)∘≠) ⊃⎕NGet ⍵ ⍝ Get the lines of the mobile from the file
mobileStart ← ⌊/ '┴│' ⍳⍨ ⊃lines ⍝ Find the start of the mobile
structure ← mobileStart parseMobile lines ⍝ Parse the mobile into a recursive structure
result ← (multReduce calcTotals ⊢) structure ⍝ Get the array of letters to weights
(∊ (⊂∘⍋⊃¨) ⌷ ⊃∘⌽¨) result ⍝ Sort the result by the letters
}
:EndNamespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment