Skip to content

Instantly share code, notes, and snippets.

@bulters
Created November 24, 2020 10:58
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 bulters/661a7389d3684826e20791c7019d3bf8 to your computer and use it in GitHub Desktop.
Save bulters/661a7389d3684826e20791c7019d3bf8 to your computer and use it in GitHub Desktop.
AoC19 Day 2 optimization attemt
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