Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. main = do
  2.     contents <- getContents
  3.     let cleanContents = takeWhile (/='\n') contents
  4.         memory = read ("[" ++ cleanContents ++ "]") :: [Int]
  5.         correctedMemory = write 12 1 $ write 2 2 memory
  6.         answer = 19690720
  7.     print [100 * noun + verb | noun <- [0..99], verb <- [0..99], answer == let testMemory = write noun 1 $ write verb 2 memory
  8.                                                                           in head $ execute 0 testMemory]
  9.  
  10. execute :: Int -> [Int] -> [Int]
  11. execute pos memory
  12.     | op == 1  = execute nextpos $ write (input1 + input2) writepos memory
  13.     | op == 2  = execute nextpos $ write (input1 * input2) writepos memory
  14.     | op == 99 = memory
  15.     | otherwise = [-1] -- TODO should be a Maybe or something...
  16.     where op = memory !! pos
  17.           nextpos = pos + 4
  18.           input1   = memory !! (memory !! (pos + 1))
  19.           input2   = memory !! (memory !! (pos + 2))
  20.           writepos = memory !! (pos + 3)
  21.  
  22. write :: Int -> Int -> [Int] -> [Int]
  23. write value pos memory =
  24.     let (front, _:back) = splitAt pos memory
  25.     in  front ++ [value] ++ back
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement