Created
November 24, 2020 10:58
-
-
Save bulters/661a7389d3684826e20791c7019d3bf8 to your computer and use it in GitHub Desktop.
AoC19 Day 2 optimization attemt
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
runProgramAt :: Int -> Memory -> Memory | |
runProgramAt pc m = let opcode = m M.! pc | |
left = m M.! (m M.! (pc + 1)) | |
right = m M.! (m M.! (pc + 2)) | |
result = m M.! (pc + 3) | |
next = pc + 4 in | |
case opcode of | |
-- 99 signals the end of execution, so just return the memory | |
99 -> m | |
-- 1 indicates addition (left + right -> result) | |
1 -> runProgramAt next $ M.alter (Just (left + right)) result m | |
-- 2 indicates multiplication (left * right -> result) | |
2 -> runProgramAt next $ M.alter (Just (left * right)) result m | |
-- otherwise, we return memory itself. | |
otherwise -> m |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment