Skip to content

Instantly share code, notes, and snippets.

@miikka
Created December 21, 2018 06:09
Show Gist options
  • Save miikka/470e7e6d4c1f645cef379b68b6fb1971 to your computer and use it in GitHub Desktop.
Save miikka/470e7e6d4c1f645cef379b68b6fb1971 to your computer and use it in GitHub Desktop.
Advent of Code 2018
left =: 4 : 0
if. y = 0 do. x - 1
else. <: y
end.
)
right =: 4 : 0
if. y = x do. 1
else. >:y
end.
)
nsum =: 3 : '(y * >: y) % 2'
play =: 4 : 0
scores =: 0 * i. x
marbles =: 0
n =: 1
p =: 0
while. n <: y do.
nm =. # marbles
if. 0 = 23 | n do.
si =. x | n
pi =. nm | p - 7
np =. n + pi { marbles
scores =. (si {. scores) , (np + si { scores), ((>:si) }. scores)
marbles =. (pi ~: i. nm) # marbles
if. pi = # marbles do. p =. 0
else. p =. pi
end.
else.
p =. nm right nm right p
marbles =. (p {. marbles) , n , (p }. marbles)
end.
if. (nsum n) ~: (+/ marbles) + (+/ scores) do.
echo 'oh no' ; n ;(nsum n) ;(+/ marbles) ;(+/ scores)
end.
NB. echo marbles ,: p = i. (# marbles)
n =. >: n
end.
echo 'marbles'; +/ marbles
echo 'scores'; scores
echo >./ scores
scores
)
play2 =: 4 : 0
scores =: 0 * i. x
lefts =: (>: y) $ _999
rights =: (>: y) $ _999
lefts =: 0 (0}) lefts
rights =: 0 (0}) rights
n =: 1
p =: 0
while. n <: y do.
if. 0 = 23 | n do.
p =. ({ & lefts)^:7 p
si =. x | n
np =. n + p + si { scores
scores =. np si} scores
NB. echo scores
oldr =. p { rights
oldl =. p { lefts
NB. echo p
NB. echo 'eh'; lefts ,: rights
lefts =. (_999 , oldl) (p , oldr)} lefts
rights =. (_999 , oldr) (p , oldl)} rights
p =. oldr
NB. 1echo 'oh';< lefts ,: rights
else.
p =. (p { rights)
oldr =. (p { rights)
rights =. (oldr , n) (n , p)} rights
lefts =. (p , n) (n , oldr)} lefts
p =. n
end.
NB. inplay =. +/ (lefts ~: _999) *. i. >: y
NB. if. (nsum n) ~: (inplay + +/scores) do.
NB. echo 'oh no' ; n ; inplay ; scores
NB. end.
NB. echo < lefts ,: rights
n =. >: n
end.
>./scores
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment