Day 15: Part 2 is solvable

Only manually, but who cares, this is a fun enough game to play and
doesn’t take very long
This commit is contained in:
shu 2019-12-15 15:30:41 +01:00
parent 58509b8d56
commit dedbad960e

View File

@ -25,8 +25,8 @@ main = do
program <- readIntcode <$> readFile "input" program <- readIntcode <$> readFile "input"
mode <- queryMode mode <- queryMode
let tm = defaultTM Nothing program let tm = defaultTM Nothing program
let run x = runGameloop $ pure (tm {state = AwaitInput}, M.empty, V2 0 20, mode) let run x = runGameloop $ pure (tm {state = AwaitInput}, M.empty, V2 0 20, 0, mode)
(_, buf, _, _) <- run 0 (_, buf, _, _, _) <- run 0
putStrLn "Done." putStrLn "Done."
queryMode :: IO Bool queryMode :: IO Bool
@ -39,10 +39,10 @@ queryMode = do
queryMode queryMode
runGameloop :: runGameloop ::
IO (TuringMachine, ScreenBuffer, V2 Int, Bool) IO (TuringMachine, ScreenBuffer, V2 Int, Integer, Bool)
-> IO (TuringMachine, ScreenBuffer, V2 Int, Bool) -> IO (TuringMachine, ScreenBuffer, V2 Int, Integer, Bool)
runGameloop io = do runGameloop io = do
(tm, buf, oldPos, mode) <- io (tm, buf, oldPos, maxStep, mode) <- io
case state tm of case state tm of
AwaitInput -> do AwaitInput -> do
nextCommand <- nextCommand <-
@ -61,15 +61,19 @@ runGameloop io = do
let newPos = case tmNewOut of let newPos = case tmNewOut of
0 -> oldPos 0 -> oldPos
_ -> newPosTry _ -> newPosTry
print tmNewOut
let bufNew = if tmNewOut == 0 then M.insert newPosTry 0 buf else buf let bufNew = if tmNewOut == 0 then M.insert newPosTry 0 buf else buf
let bufNew2 = if newPos /= oldPos then updatePath bufNew oldPos newPos else bufNew let bufNew2 = if newPos /= oldPos then updatePath bufNew oldPos newPos else bufNew
let bufNew3 = if tmNewOut == 2 then M.insert newPos 2 bufNew2 else bufNew2 if tmNewOut == 2 then print $ countSteps bufNew2 else return ()
print mode
let newMode = if tmNewOut == 2 then True else mode
let bufNew3 = if tmNewOut == 2 then M.filter (/=1) bufNew2 else bufNew2
let newMaxStep = max maxStep tmNewOut
putStrLn $ drawBoard (M.insert newPos 3 bufNew3) putStrLn $ drawBoard (M.insert newPos 3 bufNew3)
print $ countSteps bufNew2 print $ countSteps bufNew2
print newMaxStep
runGameloop $ runGameloop $
pure (tmNew {input = Just nextCommand, output = []}, bufNew3, newPos, mode) pure (tmNew {input = Just nextCommand, output = []}, bufNew3, newPos, newMaxStep, newMode)
_ -> pure (tm, buf, oldPos, mode) _ -> pure (tm, buf, oldPos, maxStep, mode)
updatePath :: ScreenBuffer -> V2 Int -> V2 Int -> ScreenBuffer updatePath :: ScreenBuffer -> V2 Int -> V2 Int -> ScreenBuffer
updatePath buf oldPos newPos = if M.member newPos buf then M.delete oldPos buf else M.insert newPos 1 buf updatePath buf oldPos newPos = if M.member newPos buf then M.delete oldPos buf else M.insert newPos 1 buf