Day5: Part2 works
This commit is contained in:
parent
e9a6550480
commit
47429d0edd
39
day5.hs
39
day5.hs
|
@ -3,7 +3,7 @@ import Data.Char as Char
|
||||||
|
|
||||||
main = do
|
main = do
|
||||||
content <- getList <$> getContents
|
content <- getList <$> getContents
|
||||||
let input = [1]
|
let input = [5]
|
||||||
let output = operation content content 0 input []
|
let output = operation content content 0 input []
|
||||||
mapM putStrLn (map show output)
|
mapM putStrLn (map show output)
|
||||||
|
|
||||||
|
@ -48,13 +48,25 @@ operation (op:x:y:z:_) state index input output
|
||||||
let newstate = put (fillup (revertdigs op) 3) x (head input) state
|
let newstate = put (fillup (revertdigs op) 3) x (head input) state
|
||||||
let newinput = drop 1 input
|
let newinput = drop 1 input
|
||||||
operation (drop newindex newstate) newstate newindex newinput output
|
operation (drop newindex newstate) newstate newindex newinput output
|
||||||
|
|
||||||
| last (digits op) == 4 = do
|
| last (digits op) == 4 = do
|
||||||
let newindex = index + 2
|
let newindex = index + 2
|
||||||
let newoutput = out (fillup (revertdigs op) 3) output x state
|
let newoutput = out (fillup (revertdigs op) 3) output x state
|
||||||
let newinput = drop 1 input
|
let newinput = drop 1 input
|
||||||
operation (drop newindex state) state newindex input newoutput
|
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 :: [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 =
|
||||||
|
@ -76,6 +88,27 @@ out :: [Int] -> [Int] -> Int -> [Int] -> [Int]
|
||||||
out (op1:op2:m1:_) output p1 state =
|
out (op1:op2:m1:_) output p1 state =
|
||||||
output ++ [(getValue m1 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) = insert state 1 p3
|
||||||
|
| otherwise = 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) = insert state 1 p3
|
||||||
|
| otherwise = insert state 0 p3
|
||||||
|
|
||||||
|
|
||||||
insert :: [Int] -> Int -> Int -> [Int]
|
insert :: [Int] -> Int -> Int -> [Int]
|
||||||
insert xs value index = do
|
insert xs value index = do
|
||||||
let split = splitAt index xs
|
let split = splitAt index xs
|
||||||
|
|
Loading…
Reference in New Issue
Block a user