Backup
This commit is contained in:
parent
0a2c29bb8c
commit
576761c82c
68
day7.hs
68
day7.hs
|
@ -1,14 +1,21 @@
|
|||
import Data.List.Split
|
||||
import Data.Char as Char
|
||||
import Data.List as List
|
||||
import Data.Either as Either
|
||||
|
||||
main = do
|
||||
software <- getList <$> getContents
|
||||
--let output = operation state state 0 input []
|
||||
let combs = getComb [4,3,2,1,0]
|
||||
let output = List.maximum ( map (\x-> calcthruster software x) combs )
|
||||
let combs = getComb [5,6,7,8,9]
|
||||
mapM putStrLn(map show combs)
|
||||
mapM putStrLn(map show ( map (\x-> part2 $ prepareAmps x software ) combs ))
|
||||
-- let output = calcthruster software [4,3,2,1,0]
|
||||
putStrLn (show output)
|
||||
|
||||
data Amplifier = Amplifier{ state :: [Int]
|
||||
,index :: Int
|
||||
,input :: [Int]
|
||||
,output :: [Int] }
|
||||
type Outputtype = Either [Int] Amplifier
|
||||
|
||||
getList :: String -> [Int]
|
||||
getList = map read . splitOn ","
|
||||
|
@ -16,19 +23,49 @@ getList = map read . splitOn ","
|
|||
getComb :: [Int] -> [[Int]]
|
||||
getComb array = filter ((5==).length.(List.nub)) $ mapM (const array) [1..5]
|
||||
|
||||
calcthruster :: [Int] -> [Int] -> Int
|
||||
calcthruster software (p1:p2:p3:p4:p5:_) = do
|
||||
let outputA = operation software software 0 [p1, 0] []
|
||||
let outputB = operation software software 0 [p2, last outputA] []
|
||||
let outputC = operation software software 0 [p3, last outputB] []
|
||||
let outputD = operation software software 0 [p4, last outputC] []
|
||||
let outputE = operation software software 0 [p5, last outputD] []
|
||||
last outputE
|
||||
prepareAmps :: [Int] -> [Int] -> [Amplifier]
|
||||
prepareAmps (p1:p2:p3:p4:p5:_) software = do
|
||||
let a = step (Amplifier software 0 [] []) [p1]
|
||||
let b = step (Amplifier software 0 [] []) [p2]
|
||||
let c = step (Amplifier software 0 [] []) [p3]
|
||||
let d = step (Amplifier software 0 [] []) [p4]
|
||||
let e = step (Amplifier software 0 [] []) [p5]
|
||||
let e = Amplifier (state e) (index e) (input e) [0]
|
||||
[a,b,c,d,e]
|
||||
|
||||
part2 :: [Amplifier] -> Int
|
||||
part2 amps = do
|
||||
let ampA = link (amps!!4) (amps!!0)
|
||||
let ampB = link (amps!!0) (amps!!1)
|
||||
let ampC = link (amps!!1) (amps!!2)
|
||||
let ampD = link (amps!!2) (amps!!3)
|
||||
let ampE = link (amps!!3) (amps!!4)
|
||||
if state ampE == [88]
|
||||
then head (output (amps!!4))
|
||||
else part2 [ampA,ampB,ampC,ampD,ampE]
|
||||
|
||||
link :: Amplifier -> Amplifier -> Amplifier
|
||||
link left calc
|
||||
| length (output left) == 0 = Amplifier ([88]) (index calc) (input calc) (output calc)
|
||||
| otherwise = step calc [last $ output left]
|
||||
|
||||
step :: Amplifier -> [Int] -> Amplifier
|
||||
step amp input = operation (drop (index amp) (state amp)) (state amp) (index amp) input []
|
||||
|
||||
|
||||
operation :: [Int] -> [Int] -> Int -> [Int] -> [Int] -> [Int]
|
||||
calcthrusters :: [Int] -> [Int] -> Int -> Int
|
||||
calcthrusters software (p1:p2:p3:p4:p5:_) start = do
|
||||
let outputA = operation software software 0 [p1, start] []
|
||||
let outputB = operation software software 0 [p2, last $ output outputA] []
|
||||
let outputC = operation software software 0 [p3, last $ output outputB] []
|
||||
let outputD = operation software software 0 [p4, last $ output outputC] []
|
||||
let outputE = operation software software 0 [p5, last $ output outputD] []
|
||||
last $ output outputE
|
||||
|
||||
|
||||
operation :: [Int] -> [Int] -> Int -> [Int] -> [Int] -> Amplifier
|
||||
operation (99:_) state index input output =
|
||||
output
|
||||
Amplifier state index input output
|
||||
operation (op:x:y:z:_) state index input output
|
||||
| last (digits op) == 1 = do
|
||||
let newindex = index + 4
|
||||
|
@ -37,7 +74,8 @@ operation (op:x:y:z:_) state index 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
|
||||
Amplifier state newindex input output
|
||||
--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
|
||||
|
@ -46,7 +84,7 @@ operation (op:x:y:z:_) state index input 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
|
||||
let newinput = drop 1 input
|
||||
operation (drop newindex state) state newindex input newoutput
|
||||
| (last (digits op) == 5 ) = do
|
||||
let newindex = jumpif (fillup (revertdigs op) 4) x y index state
|
||||
|
|
Loading…
Reference in New Issue
Block a user