From 43e2e103cb06654f595bc9c1438eb825290d1ffa Mon Sep 17 00:00:00 2001 From: Arranun Date: Thu, 5 Dec 2019 08:29:32 +0100 Subject: [PATCH] day2 --- day2.hs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 day2.hs diff --git a/day2.hs b/day2.hs new file mode 100644 index 0000000..80a51d6 --- /dev/null +++ b/day2.hs @@ -0,0 +1,35 @@ +import Data.List.Split + +main = do + content <- getList <$> getContents + let input = changeInput content 12 2 + putStrLn (show (compute input input 0)) + putStrLn (show (part2 content)) + +getList :: String -> [Int] +getList = map read . splitOn "," + +changeInput :: [Int] -> Int -> Int -> [Int] +changeInput (begin:_:_:input) input1 input2= [begin] ++ [input1] ++ [input2] ++ input + +func :: [Int] -> Int -> Int -> Int +func xs x y = do + let input = changeInput xs x y + head (compute input input 0) + +compute :: [Int] -> [Int] -> Int -> [Int] +compute (99:_) state index = state +compute (op:x:y:z:_) state index = + compute (drop newindex state) newstate newindex + where + sum = if op == 1 then + (state !! x) + (state !! y) + else + (state !! x) * (state !! y) + split = splitAt z state + newstate = (fst split) ++ [sum] ++ (drop 1 (snd split)) + newindex = index + 4 + +part2 input = [ (i,j) | i <- [1..99], + j <- [1..99], + func input i j == 19690720 ]