Backup
This commit is contained in:
parent
0a2c29bb8c
commit
576761c82c
66
day7.hs
66
day7.hs
|
@ -1,14 +1,21 @@
|
||||||
import Data.List.Split
|
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
|
||||||
|
|
||||||
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 [4,3,2,1,0]
|
let combs = getComb [5,6,7,8,9]
|
||||||
let output = List.maximum ( map (\x-> calcthruster software x) combs )
|
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]
|
-- 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 :: String -> [Int]
|
||||||
getList = map read . splitOn ","
|
getList = map read . splitOn ","
|
||||||
|
@ -16,19 +23,49 @@ getList = map read . splitOn ","
|
||||||
getComb :: [Int] -> [[Int]]
|
getComb :: [Int] -> [[Int]]
|
||||||
getComb array = filter ((5==).length.(List.nub)) $ mapM (const array) [1..5]
|
getComb array = filter ((5==).length.(List.nub)) $ mapM (const array) [1..5]
|
||||||
|
|
||||||
calcthruster :: [Int] -> [Int] -> Int
|
prepareAmps :: [Int] -> [Int] -> [Amplifier]
|
||||||
calcthruster software (p1:p2:p3:p4:p5:_) = do
|
prepareAmps (p1:p2:p3:p4:p5:_) software = do
|
||||||
let outputA = operation software software 0 [p1, 0] []
|
let a = step (Amplifier software 0 [] []) [p1]
|
||||||
let outputB = operation software software 0 [p2, last outputA] []
|
let b = step (Amplifier software 0 [] []) [p2]
|
||||||
let outputC = operation software software 0 [p3, last outputB] []
|
let c = step (Amplifier software 0 [] []) [p3]
|
||||||
let outputD = operation software software 0 [p4, last outputC] []
|
let d = step (Amplifier software 0 [] []) [p4]
|
||||||
let outputE = operation software software 0 [p5, last outputD] []
|
let e = step (Amplifier software 0 [] []) [p5]
|
||||||
last outputE
|
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 =
|
operation (99:_) state index input output =
|
||||||
output
|
Amplifier state index input output
|
||||||
operation (op:x:y:z:_) state index input output
|
operation (op:x:y:z:_) state index input output
|
||||||
| last (digits op) == 1 = do
|
| last (digits op) == 1 = do
|
||||||
let newindex = index + 4
|
let newindex = index + 4
|
||||||
|
@ -37,7 +74,8 @@ operation (op:x:y:z:_) state index input output
|
||||||
| last (digits op) == 2 = do
|
| last (digits op) == 2 = do
|
||||||
let newindex = index + 4
|
let newindex = index + 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
|
Amplifier state newindex input output
|
||||||
|
--operation (drop newindex newstate) newstate newindex input output
|
||||||
| last (digits op) == 3 = do
|
| last (digits op) == 3 = do
|
||||||
let newindex = index + 2
|
let newindex = index + 2
|
||||||
let newstate = put (fillup (revertdigs op) 3) x (head input) state
|
let newstate = put (fillup (revertdigs op) 3) x (head input) state
|
||||||
|
|
Loading…
Reference in New Issue
Block a user