day7: Ne jetzt funktioniert es wirklich
This commit is contained in:
parent
f792faf44d
commit
db092a272a
44
day7.hs
44
day7.hs
|
@ -6,9 +6,9 @@ import Data.Either as Either
|
|||
main = do
|
||||
software <- getList <$> getContents
|
||||
--let output = operation state state 0 input []
|
||||
let combs = [[9,8,7,6,5]]
|
||||
let combs = getComb [5,6,7,8,9]
|
||||
--mapM putStrLn(map show combs)
|
||||
mapM putStrLn(map show ( map (\x-> part2 ( prepareAmps x software) 0 ) combs ))
|
||||
putStrLn(show $ List.maximum ( map (\x-> part2 ( prepareAmps x software) 0 ) combs ))
|
||||
-- let output = calcthruster software [4,3,2,1,0]
|
||||
|
||||
data Amplifier = Amplifier{ state :: [Int]
|
||||
|
@ -64,42 +64,42 @@ calcthrusters software (p1:p2:p3:p4:p5:_) start = do
|
|||
operation :: [Int] -> [Int] -> Int -> [Int] -> [Int] -> Amplifier
|
||||
operation (99:_) state i input output =
|
||||
Amplifier state i input output
|
||||
operation (op:x:y:z:_) state i input output
|
||||
operation (op:xs) state i input output
|
||||
| last (digits op) == 1 = do
|
||||
let newindex = i + 4
|
||||
let newstate = add (fillup (revertdigs op) 5) x y z state
|
||||
operation (drop newindex newstate) (newstate) newindex input output
|
||||
let newstate = add (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) state
|
||||
operation ((drop newindex newstate)) (newstate) newindex input output
|
||||
| last (digits op) == 2 = do
|
||||
let newindex = i + 4
|
||||
let newstate = mult (fillup (revertdigs op) 5) x y z state
|
||||
operation ((drop newindex newstate)) (newstate) newindex input output
|
||||
let newstate = mult (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) state
|
||||
operation ((drop newindex newstate)) (newstate) newindex input output
|
||||
| last (digits op) == 3 = do
|
||||
if (length input) == 0
|
||||
then (Amplifier state i input output)
|
||||
else do
|
||||
let newindex = i + 2
|
||||
let newstate = put (fillup (revertdigs op) 3) x (head input) state
|
||||
let newstate = put (fillup (revertdigs op) 3) (xs!!0) (head input) state
|
||||
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
|
||||
let newindex = i + 2
|
||||
let newoutput = out (fillup (revertdigs op) 3) output x state
|
||||
let newoutput = out (fillup (revertdigs op) 3) output (xs!!0) state
|
||||
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 i state
|
||||
let newindex = jumpif (fillup (revertdigs op) 4) (xs!!0) (xs!!1) i state
|
||||
operation ((drop newindex state)) (state) newindex input output
|
||||
| (last (digits op) == 6 ) = do
|
||||
let newindex = jumpifnot (fillup (revertdigs op) 4) x y i state
|
||||
operation (drop newindex state) (state) newindex input output
|
||||
let newindex = jumpifnot (fillup (revertdigs op) 4) (xs!!0) (xs!!1) i state
|
||||
operation ((drop newindex state)) (state) newindex input output
|
||||
| (last (digits op) == 7 ) = do
|
||||
let newindex = i + 4
|
||||
let newstate = lessthan (fillup (revertdigs op) 5) x y z state
|
||||
operation (drop newindex newstate) (newstate) newindex input output
|
||||
let newstate = lessthan (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) state
|
||||
operation ((drop newindex newstate)) (newstate) newindex input output
|
||||
| (last (digits op) == 8 ) = do
|
||||
let newindex = i + 4
|
||||
let newstate = equal (fillup (revertdigs op) 5) x y z state
|
||||
operation (drop newindex newstate) (newstate) newindex input output
|
||||
let newstate = equal (fillup (revertdigs op) 5) (xs!!0) (xs!!1) (xs!!2) 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 =
|
||||
|
@ -110,8 +110,8 @@ add (op1:op2:m1:m2:m3:_) p1 p2 p3 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)
|
||||
where
|
||||
sum = (getValue m1 p1 state) * (getValue m2 p2 state)
|
||||
|
||||
put :: [Int] -> Int -> Int -> [Int] -> [Int]
|
||||
put(op1:op2:m1:_) p1 input state =
|
||||
|
@ -119,11 +119,11 @@ put(op1:op2:m1:_) p1 input state =
|
|||
|
||||
out :: [Int] -> [Int] -> Int -> [Int] -> [Int]
|
||||
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
|
||||
| (getValue m1 p1 state) /= 0 = getValue m2 p2 state
|
||||
| otherwise = index + 3
|
||||
|
||||
jumpifnot :: [Int] -> Int -> Int -> Int -> [Int] -> Int
|
||||
|
|
Loading…
Reference in New Issue
Block a user