From 3ebc6755af3d590e207b5330c448f4fddc56f573 Mon Sep 17 00:00:00 2001 From: Arranun Date: Thu, 5 Dec 2019 23:04:58 +0100 Subject: [PATCH] Success for test case day5 --- day5.hs | 83 +++++++++++++++++++++++++++------------------------------ 1 file changed, 39 insertions(+), 44 deletions(-) diff --git a/day5.hs b/day5.hs index d54e5ea..fbaa606 100644 --- a/day5.hs +++ b/day5.hs @@ -2,9 +2,9 @@ import Data.List.Split import Data.Char as Char main = do - let state = [3,0,4,0,99] - let input = [83] - let output = operation state state 0 input [] + let content = [1002,4,3,4,33] + let input = [] + let output = operation content content 0 input [] mapM putStrLn (map show output) getList :: String -> [Int] @@ -19,7 +19,7 @@ func xs x y = do head (compute input input 0 [] [] ) compute :: [Int] -> [Int] -> Int -> [Int] -> [Int] -> [Int] -compute (99:_) state index input output = output +compute (99:_) state index input output = state compute (op:x:y:z:_) state index input output= compute (drop newindex state) newstate newindex input output where @@ -33,53 +33,48 @@ compute (op:x:y:z:_) state index input output= operation :: [Int] -> [Int] -> Int -> [Int] -> [Int] -> [Int] operation (99:_) state index input output = - output + state operation (op:x:y:z:_) state index input output - | last (digits op) == 1 = - operation (drop newindex state) newstate newindex input output - where - newindex = index + 4 - newstate = add (fillup (revertdigs op) 5) x y z state - | last (digits op) == 2 = - operation (drop newindex state) newstate newindex input output - where - newindex = index + 4 - newstate = mult (fillup (revertdigs op) 5) x y z state - | last (digits op) == 3 = - operation (drop newindex state) newstate newindex newinput output - where - newindex = index + 2 - newstate = input (fillup (revertdigs op) 3) x (head input) state - newinput = drop 1 input - | last (digits op) == 4 = - operation (drop newindex state) state newindex input newoutput - where - newindex = index + 2 - newoutput = log (fillup (revertdigs op) 3) ouput x state - newinput = drop 1 input + | 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 add :: [Int] -> Int -> Int -> Int -> [Int] -> [Int] -add (op1:op2:m1:m2:m3) p1 p2 p3 state = - insert state sum index +add (op1:op2:m1:m2:m3:_) p1 p2 p3 state = + insert state sum p3 where - sum = (getValue m1 p1 ) + (getValue m2 p2) - index = getValue m3 p3 + 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 = - insert state sum index +mult (op1:op2:m1:m2:m3:_) p1 p2 p3 state = + insert state sum p3 where - sum = (getValue m1 p1 ) * (getValue m2 p2) - index = getValue m3 p3 + sum = (getValue m1 p1 state) * (getValue m2 p2 state) -input :: [Int] -> Int -> Int -> [Int] -> [Int] -input(op1:op2:m1) p1 input state = - insert state input (getValue m1 p1) +put :: [Int] -> Int -> Int -> [Int] -> [Int] +put(op1:op2:m1:_) p1 input state = + insert state input p1 -log :: [Int] -> [Int] -> Int -> [Int] -> [Int] -log (op1:op2:m1) output p1 state = - output ++ [(state !! (getValue m1 p1))] +out :: [Int] -> [Int] -> Int -> [Int] -> [Int] +out (op1:op2:m1:_) output p1 state = + output ++ [(state !! (getValue m1 p1 state))] insert :: [Int] -> Int -> Int -> [Int] insert xs value index = do @@ -90,11 +85,11 @@ digits :: Int -> [Int] digits = map Char.digitToInt . show revertdigs :: Int -> [Int] -digs 0 = [] -digs x = x `mod` 10 : digs (x `div` 10) +revertdigs 0 = [] +revertdigs x = x `mod` 10 : revertdigs (x `div` 10) fillup :: [Int] -> Int -> [Int] -fillup array x = x ++ (replicate (x - (length array)) 0) +fillup array x = array ++ (replicate (x - (length array)) 0) getValue :: Int -> Int -> [Int] -> Int getValue 0 index array = array !! index