From dedbad960e78ff87c52406efbf439f9454926647 Mon Sep 17 00:00:00 2001 From: shu Date: Sun, 15 Dec 2019 15:30:41 +0100 Subject: [PATCH] Day 15: Part 2 is solvable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Only manually, but who cares, this is a fun enough game to play and doesn’t take very long --- 2019/day15/day15.hs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/2019/day15/day15.hs b/2019/day15/day15.hs index 5f14035..69bb484 100644 --- a/2019/day15/day15.hs +++ b/2019/day15/day15.hs @@ -25,8 +25,8 @@ main = do program <- readIntcode <$> readFile "input" mode <- queryMode let tm = defaultTM Nothing program - let run x = runGameloop $ pure (tm {state = AwaitInput}, M.empty, V2 0 20, mode) - (_, buf, _, _) <- run 0 + let run x = runGameloop $ pure (tm {state = AwaitInput}, M.empty, V2 0 20, 0, mode) + (_, buf, _, _, _) <- run 0 putStrLn "Done." queryMode :: IO Bool @@ -39,10 +39,10 @@ queryMode = do queryMode runGameloop :: - IO (TuringMachine, ScreenBuffer, V2 Int, Bool) - -> IO (TuringMachine, ScreenBuffer, V2 Int, Bool) + IO (TuringMachine, ScreenBuffer, V2 Int, Integer, Bool) + -> IO (TuringMachine, ScreenBuffer, V2 Int, Integer, Bool) runGameloop io = do - (tm, buf, oldPos, mode) <- io + (tm, buf, oldPos, maxStep, mode) <- io case state tm of AwaitInput -> do nextCommand <- @@ -61,15 +61,19 @@ runGameloop io = do let newPos = case tmNewOut of 0 -> oldPos _ -> newPosTry - print tmNewOut 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 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) print $ countSteps bufNew2 + print newMaxStep runGameloop $ - pure (tmNew {input = Just nextCommand, output = []}, bufNew3, newPos, mode) - _ -> pure (tm, buf, oldPos, mode) + pure (tmNew {input = Just nextCommand, output = []}, bufNew3, newPos, newMaxStep, newMode) + _ -> pure (tm, buf, oldPos, maxStep, mode) 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