day 3 ivy solution
parent
7aece65dcb
commit
d1b96f1324
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,48 @@
|
|||
) get "input.ivy"
|
||||
|
||||
alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
op vec str = or/ alpha o.== str
|
||||
|
||||
# find the first occurence of an element in a vector
|
||||
op c index str = +/ and\ c != str
|
||||
|
||||
# reshapes a vector as a 1-row matrix
|
||||
op lift v = (1, rho v) rho v
|
||||
|
||||
op a concat b =
|
||||
0 == rho rho a: b
|
||||
0 == rho rho b: a
|
||||
a, b
|
||||
|
||||
nl = "\n"
|
||||
null = 0 rho 0
|
||||
|
||||
op m parse input =
|
||||
(rho input) == 0: m # if input is empty return the matrix
|
||||
i = nl index input # find the first newline
|
||||
k = (i+1) min (rho input)
|
||||
line = i take input # get chars before the newline
|
||||
rest = k drop input # get chars after the newline
|
||||
n = (rho line) / 2 # split line into two equal pieces
|
||||
A = vec n take line # and convert them to 1-hot vectors
|
||||
B = vec n drop line
|
||||
C = (lift A), (lift B) # make a pair
|
||||
m = m concat lift C # add the pair to the accumulator matrix
|
||||
m parse rest # recurse
|
||||
|
||||
op parse input = null parse input
|
||||
|
||||
op score m = +/ (iota rho alpha) * +/ transp m
|
||||
|
||||
op solve m =
|
||||
score m[;1] and m[;2]
|
||||
|
||||
op solve2 m =
|
||||
m = m[;1] or m[;2]
|
||||
n = (rho m)[1] / 3
|
||||
i = -2 + 3 * iota n
|
||||
score m[i] and m[i+1] and m[i+2]
|
||||
|
||||
m = parse input
|
||||
solve m
|
||||
solve2 m
|
Loading…
Reference in New Issue