mirror of
https://github.com/mx42/adventofcode.git
synced 2026-01-14 22:09:50 +01:00
Add WIP day7 part 2
This commit is contained in:
@@ -7,24 +7,35 @@ processProgram :: [Int] -> [Int] -> Int
|
||||
processProgram program inputs = head outputs
|
||||
where outputs = runProgramV2 inputs program
|
||||
|
||||
-- Memory -> phase (1st input) -> 2nd input -> outputs
|
||||
chainProcesses :: [Int] -> [Int] -> Int -> Int
|
||||
chainProcesses program (phase:phases) signal
|
||||
| null phases = newSignal
|
||||
| otherwise = chainProcesses program phases newSignal
|
||||
chainProcesses program (phase:nextPhases) signal
|
||||
| (null nextPhases) = newSignal
|
||||
| otherwise = chainProcesses program nextPhases newSignal
|
||||
where newSignal = processProgram program [phase, signal]
|
||||
|
||||
testCombinations :: [Int] -> [([Int], Int)]
|
||||
testCombinations program = map (\p -> (p, chainProcesses program p 0)) phasesPerm
|
||||
testCombinationsP1 :: [Int] -> [([Int], Int)]
|
||||
testCombinationsP1 program = map (\p -> (p, chainProcesses program p 0)) phasesPerm
|
||||
where phasesPerm = permutations [0..4]
|
||||
|
||||
testCombinationsP2 :: [Int] -> [([Int], Int)]
|
||||
testCombinationsP2 program = map (\p -> (p, runProgramV3 program p)) phasesPerm
|
||||
where phasesPerm = permutations [5..9]
|
||||
|
||||
day7 :: IO ()
|
||||
day7 = do
|
||||
putStrLn $ "AoC 2019 day 7"
|
||||
input <- getLine
|
||||
|
||||
let program = parseProgram input
|
||||
let combinations = testCombinations program
|
||||
let combinationsP1 = testCombinationsP1 program
|
||||
|
||||
let p1 = maximumBy (\(_, a) (_, b) -> compare a b) combinations
|
||||
let p1 = maximumBy (\(_, a) (_, b) -> compare a b) combinationsP1
|
||||
|
||||
putStrLn $ "Part 1: " ++ (show p1)
|
||||
|
||||
-- WIP
|
||||
let combinationsP2 = testCombinationsP2 program
|
||||
let p2 = maximumBy (\(_, a) (_, b) -> compare a b) combinationsP2
|
||||
|
||||
putStrLn $ "Part 2 (WIP/Buggy): " ++ (show p2)
|
||||
|
||||
Reference in New Issue
Block a user