-
-
Save jcmorrow/59e864fc664e0552375d724152f05dc0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
input =: ,.LF cut fread '~/code/advent_of_code/day_14.txt' | |
imem =: ".@>@(1&{)@('\d+'&rxmatch rxcut ]) | |
parse =: monad define | |
if. (<'mask') = {. y do. | |
'mask';>_1{ y return. | |
else. | |
(imem >{. y);(". >_1{ y) return. | |
end. | |
) | |
instructions =: parse &.> (' = '&rxmatches rxcut ]) &.> input | |
b36 =: monad : '((36 - #(#: y)) # 0),(#: y)' | |
present =: monad : '(-.(''X'' = y)) # i.36' | |
values =: monad : ';". &.> <"0((present y) { y)' | |
maskVal =: dyad : '(values x) (present x) } y' | |
combinations =: dyad : '(;": &>y) (xis x) } x' | |
NB. PART I | |
operate =: monad define | |
if. (<'mask') = {. y do. | |
NB. update mask | |
mask =: >(_1{ y) | |
36 # 0 return. | |
else. | |
ind =. >{. y | |
memory =: (ind;(<mask maskVal (b36 (>_1{ y))));memory | |
mask maskVal (b36 (>_1{ y)) | |
end. | |
) | |
memory =: <(0;((<36 # 0))) | |
mask =: 36 # 'X' | |
operate &.> instructions | |
uniq =: ~. >{. &> memory | |
smoutput +/ #.@>@(1&{) &> ((>{. &> memory) i. uniq) { memory | |
NB. PART II | |
operate =: monad define | |
if. (<'mask') = {. y do. | |
mask =: >(_1{ y) | |
36 # 0 return. | |
else. | |
ind =. >{. y | |
merged =. ({&a.)@(48&+) (b36 ind) | |
merged =. ('1') (ones mask)} merged | |
merged =. ('X') (xis mask)} merged | |
addresses =. #. "1 >". L:0 &.> ,.merged&combinations &> (<"1 perm(#xis merged)) | |
value =: <(b36 (>_1{ y)) | |
memory =: (,(;&value) &.> addresses),memory | |
mask maskVal (b36 (>_1{ y)) | |
end. | |
) | |
memory =: <(0;((<36 # 0))) | |
mask =: 36 # 'X' | |
operate &.> instructions | |
uniq =: ~. >{. &> memory | |
+/ #.@>@(1&{) &> ((>{. &> memory) i. uniq) { memory |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment