Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main = do
- contents <- getContents
- let cleanContents = takeWhile (/='\n') contents
- memory = read ("[" ++ cleanContents ++ "]") :: [Int]
- correctedMemory = write 12 1 $ write 2 2 memory
- answer = 19690720
- print [100 * noun + verb | noun <- [0..99], verb <- [0..99], answer == let testMemory = write noun 1 $ write verb 2 memory
- in head $ execute 0 testMemory]
- execute :: Int -> [Int] -> [Int]
- execute pos memory
- | op == 1 = execute nextpos $ write (input1 + input2) writepos memory
- | op == 2 = execute nextpos $ write (input1 * input2) writepos memory
- | op == 99 = memory
- | otherwise = [-1] -- TODO should be a Maybe or something...
- where op = memory !! pos
- nextpos = pos + 4
- input1 = memory !! (memory !! (pos + 1))
- input2 = memory !! (memory !! (pos + 2))
- writepos = memory !! (pos + 3)
- write :: Int -> Int -> [Int] -> [Int]
- write value pos memory =
- let (front, _:back) = splitAt pos memory
- in front ++ [value] ++ back
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement