Success for test case day5

This commit is contained in:
Arranun 2019-12-05 23:04:58 +01:00
parent b0f61a78e1
commit 3ebc6755af

83
day5.hs
View File

@ -2,9 +2,9 @@ import Data.List.Split
import Data.Char as Char import Data.Char as Char
main = do main = do
let state = [3,0,4,0,99] let content = [1002,4,3,4,33]
let input = [83] let input = []
let output = operation state state 0 input [] let output = operation content content 0 input []
mapM putStrLn (map show output) mapM putStrLn (map show output)
getList :: String -> [Int] getList :: String -> [Int]
@ -19,7 +19,7 @@ func xs x y = do
head (compute input input 0 [] [] ) head (compute input input 0 [] [] )
compute :: [Int] -> [Int] -> Int -> [Int] -> [Int] -> [Int] 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 (op:x:y:z:_) state index input output=
compute (drop newindex state) newstate newindex input output compute (drop newindex state) newstate newindex input output
where where
@ -33,53 +33,48 @@ compute (op:x:y:z:_) state index input output=
operation :: [Int] -> [Int] -> Int -> [Int] -> [Int] -> [Int] operation :: [Int] -> [Int] -> Int -> [Int] -> [Int] -> [Int]
operation (99:_) state index input output = operation (99:_) state index input output =
output state
operation (op:x:y:z:_) state index input output operation (op:x:y:z:_) state index input output
| last (digits op) == 1 = | last (digits op) == 1 = do
operation (drop newindex state) newstate newindex input output let newindex = index + 4
where let newstate = add (fillup (revertdigs op) 5) x y z state
newindex = index + 4 operation (drop newindex newstate) newstate newindex input output
newstate = add (fillup (revertdigs op) 5) x y z state | last (digits op) == 2 = do
| last (digits op) == 2 = let newindex = index + 4
operation (drop newindex state) newstate newindex input output let newstate = mult (fillup (revertdigs op) 5) x y z state
where operation (drop newindex newstate) newstate newindex input output
newindex = index + 4 | last (digits op) == 3 = do
newstate = mult (fillup (revertdigs op) 5) x y z state let newindex = index + 2
| last (digits op) == 3 = let newstate = put (fillup (revertdigs op) 3) x (head input) state
operation (drop newindex state) newstate newindex newinput output let newinput = drop 1 input
where operation (drop newindex newstate) newstate newindex newinput output
newindex = index + 2
newstate = input (fillup (revertdigs op) 3) x (head input) state | last (digits op) == 4 = do
newinput = drop 1 input let newindex = index + 2
| last (digits op) == 4 = let newoutput = out (fillup (revertdigs op) 3) output x state
operation (drop newindex state) state newindex input newoutput let newinput = drop 1 input
where operation (drop newindex state) state newindex input newoutput
newindex = index + 2
newoutput = log (fillup (revertdigs op) 3) ouput x state
newinput = drop 1 input
add :: [Int] -> Int -> Int -> Int -> [Int] -> [Int] add :: [Int] -> Int -> Int -> Int -> [Int] -> [Int]
add (op1:op2:m1:m2:m3) p1 p2 p3 state = add (op1:op2:m1:m2:m3:_) p1 p2 p3 state =
insert state sum index insert state sum p3
where where
sum = (getValue m1 p1 ) + (getValue m2 p2) sum = (getValue m1 p1 state) + (getValue m2 p2 state)
index = getValue m3 p3
mult :: [Int] -> Int -> Int -> Int -> [Int] -> [Int] mult :: [Int] -> Int -> Int -> Int -> [Int] -> [Int]
mult (op1:op2:m1:m2:m3) p1 p2 p3 state = mult (op1:op2:m1:m2:m3:_) p1 p2 p3 state =
insert state sum index insert state sum p3
where where
sum = (getValue m1 p1 ) * (getValue m2 p2) sum = (getValue m1 p1 state) * (getValue m2 p2 state)
index = getValue m3 p3
input :: [Int] -> Int -> Int -> [Int] -> [Int] put :: [Int] -> Int -> Int -> [Int] -> [Int]
input(op1:op2:m1) p1 input state = put(op1:op2:m1:_) p1 input state =
insert state input (getValue m1 p1) insert state input p1
log :: [Int] -> [Int] -> Int -> [Int] -> [Int] out :: [Int] -> [Int] -> Int -> [Int] -> [Int]
log (op1:op2:m1) output p1 state = out (op1:op2:m1:_) output p1 state =
output ++ [(state !! (getValue m1 p1))] output ++ [(state !! (getValue m1 p1 state))]
insert :: [Int] -> Int -> Int -> [Int] insert :: [Int] -> Int -> Int -> [Int]
insert xs value index = do insert xs value index = do
@ -90,11 +85,11 @@ digits :: Int -> [Int]
digits = map Char.digitToInt . show digits = map Char.digitToInt . show
revertdigs :: Int -> [Int] revertdigs :: Int -> [Int]
digs 0 = [] revertdigs 0 = []
digs x = x `mod` 10 : digs (x `div` 10) revertdigs x = x `mod` 10 : revertdigs (x `div` 10)
fillup :: [Int] -> Int -> [Int] 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 :: Int -> Int -> [Int] -> Int
getValue 0 index array = array !! index getValue 0 index array = array !! index