day7: degug

This commit is contained in:
Arranun 2019-12-07 22:27:53 +01:00
parent 8878956046
commit f725b27953

37
day7.hs
View File

@ -2,21 +2,20 @@ import Data.List.Split
import Data.Char as Char import Data.Char as Char
import Data.List as List import Data.List as List
import Data.Either as Either import Data.Either as Either
import Debug.Trace as Debug
main = do main = do
software <- getList <$> getContents software <- getList <$> getContents
--let output = operation state state 0 input [] --let output = operation state state 0 input []
let combs = getComb [5,6,7,8,9] let combs = [[9,8,7,6,5]]
mapM putStrLn(map show combs) --mapM putStrLn(map show combs)
mapM putStrLn(map show ( map (\x-> part2 $ prepareAmps x software ) combs )) mapM putStrLn(map show ( map (\x-> part2 $ prepareAmps x software ) combs ))
-- let output = calcthruster software [4,3,2,1,0] -- let output = calcthruster software [4,3,2,1,0]
data Amplifier = Amplifier{ state :: [Int] data Amplifier = Amplifier{ state :: [Int]
,index :: Int ,index :: Int
,input :: [Int] ,input :: [Int]
,output :: [Int] } ,output :: [Int] } deriving Show
type Outputtype = Either [Int] Amplifier
getList :: String -> [Int] getList :: String -> [Int]
getList = map read . splitOn "," getList = map read . splitOn ","
@ -42,12 +41,12 @@ part2 amps = do
let ampE = link (ampD) (amps!!4) let ampE = link (ampD) (amps!!4)
if state ampE == [88] if state ampE == [88]
then head (output (amps!!4)) then head (output (amps!!4))
else part2 [ampA,ampB,ampC,ampD,ampE] else part2 (Debug.traceShowId([ampA,ampB,ampC,ampD,ampE]))
link :: Amplifier -> Amplifier -> Amplifier link :: Amplifier -> Amplifier -> Amplifier
link left calc link left calc
| length (output left) == 0 = Amplifier ([88]) (index calc) (input calc) (output calc) | null (output left) = Amplifier ([88]) (index calc) (input calc) (output calc)
| otherwise = step calc [last $ output left] | otherwise = step calc (Debug.traceShowId([last $ output left]))
step :: Amplifier -> [Int] -> Amplifier step :: Amplifier -> [Int] -> Amplifier
step amp input = operation (drop (index amp) (state amp)) (state amp) (index amp) input [] step amp input = operation (drop (index amp) (state amp)) (state amp) (index amp) input []
@ -65,43 +64,43 @@ calcthrusters software (p1:p2:p3:p4:p5:_) start = do
operation :: [Int] -> [Int] -> Int -> [Int] -> [Int] -> Amplifier operation :: [Int] -> [Int] -> Int -> [Int] -> [Int] -> Amplifier
operation (99:_) state i input output = operation (99:_) state i input output =
Amplifier state i input [] Amplifier state i input output
operation (op:x:y:z:_) state i input output operation (op:x:y:z:_) state i input output
| last (digits op) == 1 = do | last (digits op) == 1 = do
let newindex = i + 4 let newindex = i + 4
let newstate = add (fillup (revertdigs op) 5) x y z state let newstate = add (fillup (revertdigs op) 5) x y z state
operation (drop newindex newstate) newstate newindex input output operation (Debug.traceShowId(drop newindex newstate)) (Debug.traceShowId(newstate)) newindex input output
| last (digits op) == 2 = do | last (digits op) == 2 = do
let newindex = i + 4 let newindex = i + 4
let newstate = mult (fillup (revertdigs op) 5) x y z state let newstate = mult (fillup (revertdigs op) 5) x y z state
operation (drop newindex newstate) newstate newindex input output operation (Debug.traceShowId((drop newindex newstate))) (Debug.traceShowId(newstate)) newindex input output
| last (digits op) == 3 = do | last (digits op) == 3 = do
if length input == 0 if (Debug.traceShowId(length input)) == 0
then Amplifier state i input output then (Debug.traceShowId(Amplifier state i input output))
else do else do
let newindex = i + 2 let newindex = i + 2
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 (Debug.traceShowId((drop newindex newstate))) (Debug.traceShowId(newstate)) newindex newinput output
| last (digits op) == 4 = do | last (digits op) == 4 = do
let newindex = i + 2 let newindex = i + 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 (Debug.traceShowId((drop newindex state))) (Debug.traceShowId(state)) newindex input (newoutput)
| (last (digits op) == 5 ) = do | (last (digits op) == 5 ) = do
let newindex = jumpif (fillup (revertdigs op) 4) x y i state let newindex = jumpif (fillup (revertdigs op) 4) x y i state
operation (drop newindex state) state newindex input output operation (Debug.traceShowId((drop newindex state))) (Debug.traceShowId(state)) newindex input output
| (last (digits op) == 6 ) = do | (last (digits op) == 6 ) = do
let newindex = jumpifnot (fillup (revertdigs op) 4) x y i state let newindex = jumpifnot (fillup (revertdigs op) 4) x y i state
operation (drop newindex state) state newindex input output operation (Debug.traceShowId((drop newindex state))) (Debug.traceShowId(state)) newindex input output
| (last (digits op) == 7 ) = do | (last (digits op) == 7 ) = do
let newindex = i + 4 let newindex = i + 4
let newstate = lessthan (fillup (revertdigs op) 5) x y z state let newstate = lessthan (fillup (revertdigs op) 5) x y z state
operation (drop newindex newstate) newstate newindex input output operation (Debug.traceShowId((drop newindex newstate))) (Debug.traceShowId(newstate)) newindex input output
| (last (digits op) == 8 ) = do | (last (digits op) == 8 ) = do
let newindex = i + 4 let newindex = i + 4
let newstate = equal (fillup (revertdigs op) 5) x y z state let newstate = equal (fillup (revertdigs op) 5) x y z state
operation (drop newindex newstate) newstate newindex input output operation (Debug.traceShowId((drop newindex newstate))) (Debug.traceShowId(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 =