import Data.List.Split import Data.Char as Char import Data.List as List main = do software <- getList <$> getContents --let output = operation state state 0 input [] let combs = getComb [4,3,2,1,0] let output = List.maximum ( map (\x-> calcthruster software x) combs ) -- let output = calcthruster software [4,3,2,1,0] putStrLn (show output) getList :: String -> [Int] getList = map read . splitOn "," getComb :: [Int] -> [[Int]] getComb array = filter ((5==).length.(List.nub)) $ mapM (const array) [1..5] calcthruster :: [Int] -> [Int] -> Int calcthruster software (p1:p2:p3:p4:p5:_) = do let outputA = operation software software 0 [p1, 0] [] let outputB = operation software software 0 [p2, last outputA] [] let outputC = operation software software 0 [p3, last outputB] [] let outputD = operation software software 0 [p4, last outputC] [] let outputE = operation software software 0 [p5, last outputD] [] last outputE operation :: [Int] -> [Int] -> Int -> [Int] -> [Int] -> [Int] operation (99:_) state index input output = output operation (op:x:y:z:_) state index input output | last (digits op) == 1 = do let newindex = index + 4 let newstate = add (fillup (revertdigs op) 5) x y z state operation (drop newindex newstate) newstate newindex input output | last (digits op) == 2 = do let newindex = index + 4 let newstate = mult (fillup (revertdigs op) 5) x y z state operation (drop newindex newstate) newstate newindex input output | last (digits op) == 3 = do let newindex = index + 2 let newstate = put (fillup (revertdigs op) 3) x (head input) state let newinput = drop 1 input operation (drop newindex newstate) newstate newindex newinput output | last (digits op) == 4 = do let newindex = index + 2 let newoutput = out (fillup (revertdigs op) 3) output x state let newinput = drop 1 input operation (drop newindex state) state newindex input newoutput | (last (digits op) == 5 ) = do let newindex = jumpif (fillup (revertdigs op) 4) x y index state operation (drop newindex state) state newindex input output | (last (digits op) == 6 ) = do let newindex = jumpifnot (fillup (revertdigs op) 4) x y index state operation (drop newindex state) state newindex input output | (last (digits op) == 7 ) = do let newindex = index + 4 let newstate = lessthan (fillup (revertdigs op) 5) x y z state operation (drop newindex newstate) newstate newindex input output | (last (digits op) == 8 ) = do let newindex = index + 4 let newstate = equal (fillup (revertdigs op) 5) x y z state operation (drop newindex newstate) newstate newindex input output add :: [Int] -> Int -> Int -> Int -> [Int] -> [Int] add (op1:op2:m1:m2:m3:_) p1 p2 p3 state = Main.insert state sum p3 where sum = (getValue m1 p1 state) + (getValue m2 p2 state) mult :: [Int] -> Int -> Int -> Int -> [Int] -> [Int] mult (op1:op2:m1:m2:m3:_) p1 p2 p3 state = Main.insert state sum p3 where sum = (getValue m1 p1 state) * (getValue m2 p2 state) put :: [Int] -> Int -> Int -> [Int] -> [Int] put(op1:op2:m1:_) p1 input state = Main.insert state input p1 out :: [Int] -> [Int] -> Int -> [Int] -> [Int] out (op1:op2:m1:_) output p1 state = output ++ [(getValue m1 p1 state)] jumpif :: [Int] -> Int -> Int -> Int -> [Int] -> Int jumpif (op1:op2:m1:m2:_) p1 p2 index state | (getValue m1 p1 state) /= 0 = getValue m2 p2 state | otherwise = index + 3 jumpifnot :: [Int] -> Int -> Int -> Int -> [Int] -> Int jumpifnot (op1:op2:m1:m2:_) p1 p2 index state | (getValue m1 p1 state) == 0 = getValue m2 p2 state | otherwise = index + 3 lessthan :: [Int] -> Int -> Int -> Int -> [Int] -> [Int] lessthan (op1:op2:m1:m2:m3:_) p1 p2 p3 state | (getValue m1 p1 state) < (getValue m2 p2 state) = Main.insert state 1 p3 | otherwise = Main.insert state 0 p3 equal :: [Int] -> Int -> Int -> Int -> [Int] -> [Int] equal (op1:op2:m1:m2:m3:_) p1 p2 p3 state | (getValue m1 p1 state) == (getValue m2 p2 state) = Main.insert state 1 p3 | otherwise = Main.insert state 0 p3 insert :: [Int] -> Int -> Int -> [Int] insert xs value index = do let split = splitAt index xs (fst split)++ [value] ++ (drop 1 (snd split)) digits :: Int -> [Int] digits = map Char.digitToInt . show revertdigs :: Int -> [Int] revertdigs 0 = [] revertdigs x = x `mod` 10 : revertdigs (x `div` 10) fillup :: [Int] -> Int -> [Int] fillup array x = array ++ (replicate (x - (length array)) 0) getValue :: Int -> Int -> [Int] -> Int getValue 0 index array = array !! index getValue 1 index array = index