AoC2019/day5.hs

68 lines
2.3 KiB
Haskell
Raw Normal View History

2019-12-05 18:35:01 +01:00
import Data.List.Split
main = do
2019-12-05 19:11:49 +01:00
let state = [3,0,4,0,99]
let input = [83]
let output = operation state state 0 input []
mapM putStrLn (map show output)
2019-12-05 18:35:01 +01:00
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
2019-12-05 19:11:49 +01:00
head (compute input input 0 [] [] )
2019-12-05 18:35:01 +01:00
2019-12-05 19:11:49 +01:00
compute :: [Int] -> [Int] -> Int -> [Int] -> [Int] -> [Int]
compute (99:_) state index input output = output
compute (op:x:y:z:_) state index input output=
compute (drop newindex state) newstate newindex input output
2019-12-05 18:35:01 +01:00
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
2019-12-05 19:11:49 +01:00
operation :: [Int] -> [Int] -> Int -> [Int] -> [Int] -> [Int]
operation (99:_) state index input output =
output
operation (1:x:y:z:_) state index input output =
operation (drop newindex state) newstate newindex input output
2019-12-05 18:35:01 +01:00
where
sum = (state !! x) + (state !! y)
split = splitAt z state
newstate = (fst split) ++ [sum] ++ (drop 1(snd split))
newindex = index + 4
2019-12-05 19:11:49 +01:00
operation (2:x:y:z:_) state index input output =
operation (drop newindex state) newstate newindex input output
where
2019-12-05 18:35:01 +01:00
sum = (state !! x) * (state !! y)
split = splitAt z state
newstate = (fst split) ++ [sum] ++ (drop 1 (snd split))
newindex = index + 4
2019-12-05 19:11:49 +01:00
operation (3:z:_) state index input output=
operation (drop newindex state) newstate newindex newinput output
2019-12-05 18:35:01 +01:00
where
split = splitAt z state
2019-12-05 19:11:49 +01:00
newstate = insert state (head input) z
newinput = drop 1 input
2019-12-05 18:35:01 +01:00
newindex = index + 2
2019-12-05 19:11:49 +01:00
operation (4:z:_) state index input output=
operation (drop newindex state) state newindex input newoutput
where
newoutput = output ++ [(state !! z)]
newindex = index + 2
2019-12-05 18:35:01 +01:00
insert :: [Int] -> Int -> Int -> [Int]
insert xs value index = do
let split = splitAt index xs
(fst split)++ [value] ++ (drop 1 (snd split))