diff --git a/2019/day11/Intcode.hs b/2019/day11/Intcode.hs index 7696ed2..ab1878c 100644 --- a/2019/day11/Intcode.hs +++ b/2019/day11/Intcode.hs @@ -10,7 +10,6 @@ module Intcode import Control.DeepSeq as DeepSeq import Data.Char import Data.List as L -import Debug.Trace import Data.Maybe import Data.Vector as V hiding ( (++) diff --git a/2019/day11/day11.hs b/2019/day11/day11.hs index 535090b..119c01a 100644 --- a/2019/day11/day11.hs +++ b/2019/day11/day11.hs @@ -1,18 +1,19 @@ module Main where -import Intcode +import Data.List import Data.List.Split import qualified Data.Map.Strict as M import qualified Data.Vector as V +import Intcode import Linear.V2 -import Debug.Trace data RoboState = RoboState { hull :: M.Map (V2 Int) Integer , position :: V2 Int , direction :: V2 Int - } deriving Show + } + deriving (Show) main :: IO () main = do @@ -20,35 +21,50 @@ main = do let program = V.fromList $ concatMap (map read . splitOn ",") (lines content) let run x = runIntcode - (TM - { tape = tapePreprocess program - , pointer = 0 - , pointerOffset = 0 - , output = [] - , input = Just 0 - , state = Continue - }, RoboState {hull = M.empty, position = V2 0 0, direction = V2 0 1 }) - print "hue" + ( TM + { tape = tapePreprocess program + , pointer = 0 + , pointerOffset = 0 + , output = [] + , input = Just x + , state = Continue + } + , RoboState {hull = M.empty, position = V2 0 0, direction = V2 0 1}) print $ length $ hull $ snd $ run 0 - -- concat - -- ["Part 1: " ++ show a ++ ", Part 2: " ++ show b | a <- out1, b <- out2] + putStrLn $ drawHull $ hull $ snd $ run 1 + +drawHull :: M.Map (V2 Int) Integer -> String +drawHull m = + intercalate "\n" $ + transpose $ + map reverse $ + chunksOf + 60 + [ case M.findWithDefault 0 (V2 x y) m of + 1 -> '•' + _ -> ' ' + | x <- [(-10) .. 49] + , y <- [(-10) .. 49] + ] runIntcode :: (TuringMachine, RoboState) -> (TuringMachine, RoboState) -runIntcode (tm,rb) = +runIntcode (tm, rb) = case state tmNew of - Continue -> runIntcode (tmNew,rb) - AwaitInput -> runIntcode $ updateState (tmNew,rb) - _ -> (tmNew,rb) + Continue -> runIntcode (tmNew, rb) + AwaitInput -> runIntcode $ updateState (tmNew, rb) + _ -> (tmNew, rb) where tmNew = execSteps tm updateState :: (TuringMachine, RoboState) -> (TuringMachine, RoboState) updateState (tm, rb) = (tmNew, rbNew) - where hullNew = M.insert (position rb) (output tm !! 1) (hull rb) - dirNew = if output tm !! 0 == 0 - then perp $ direction rb - else iterate perp (direction rb) !! 3 - posNew = (position rb + dirNew) - floorNext = M.findWithDefault 0 posNew (hull rb) - rbNew = rb{hull = hullNew, position = posNew, direction = dirNew} - tmNew = tm{input = Just floorNext, output = []} + where + hullNew = M.insert (position rb) (output tm !! 1) (hull rb) + dirNew = + if head (output tm) == 0 + then perp $ direction rb + else iterate perp (direction rb) !! 3 + posNew = position rb + dirNew + floorNext = M.findWithDefault 0 posNew (hull rb) + rbNew = rb {hull = hullNew, position = posNew, direction = dirNew} + tmNew = tm {input = Just floorNext, output = []}