From 3cf5c0179dd5480f0cc71ed6343ac0d87b02d5a9 Mon Sep 17 00:00:00 2001 From: shu Date: Fri, 13 Dec 2019 10:45:20 +0100 Subject: [PATCH] Day 11: Find bounds, print map, move to Helpers --- 2019/day11/Helpers.hs | 41 +++++++++++++++++++++++++++++++++++++++++ 2019/day11/day11.hs | 18 ++---------------- 2 files changed, 43 insertions(+), 16 deletions(-) create mode 100644 2019/day11/Helpers.hs diff --git a/2019/day11/Helpers.hs b/2019/day11/Helpers.hs new file mode 100644 index 0000000..68fcb9b --- /dev/null +++ b/2019/day11/Helpers.hs @@ -0,0 +1,41 @@ +module Helpers + ( v2x + , v2y + , drawMap + ) where + +import Data.List +import Data.List.Split +import qualified Data.Map as M +import Linear.V2 + +v2x :: V2 a -> a +v2x (V2 x _) = x + +v2y :: V2 a -> a +v2y (V2 _ y) = y + +getBounds :: M.Map (V2 Int) Integer -> (Int, Int, Int, Int) +getBounds m = (getMinX, getMinY, getMaxX, getMaxY) + where + getMaxX = getFromMap maximum v2x + getMaxY = getFromMap maximum v2y + getMinX = getFromMap minimum v2x + getMinY = getFromMap minimum v2y + f f1 f2 k a result = f1 [f2 k, result] + getFromMap f1 f2 = M.foldrWithKey (f f1 f2) 0 m + +drawMap :: M.Map Integer Char -> M.Map (V2 Int) Integer -> String +drawMap dict m = + intercalate "\n" $ + transpose $ + map reverse $ + chunksOf + (abs (abs y1 - abs y2) + 1) + [ let c = M.findWithDefault (-99) (V2 x y) m + in M.findWithDefault ' ' c dict + | x <- [x1 .. x2] + , y <- [y1 .. y2] + ] + where + (x1, y1, x2, y2) = getBounds m diff --git a/2019/day11/day11.hs b/2019/day11/day11.hs index 17c9a1b..efa4b1c 100644 --- a/2019/day11/day11.hs +++ b/2019/day11/day11.hs @@ -1,12 +1,11 @@ module Main where -import Data.List import Data.List.Split import qualified Data.Map.Strict as M import qualified Data.Vector as V import Intcode +import Helpers import Linear.V2 -import Control.Lens data RoboState = RoboState @@ -32,21 +31,8 @@ main = do } , RoboState {hull = M.empty, position = V2 0 0, direction = V2 0 1}) print $ length $ hull $ snd $ run 0 - putStrLn $ drawHull $ M.filter (==1) $ hull $ snd $ run 1 + putStrLn $ drawMap (M.singleton 1 '•') $ M.filter (==1) $ hull $ snd $ run 1 -drawHull :: M.Map (V2 Int) Integer -> String -drawHull m = - intercalate "\n" $ - transpose $ - map reverse $ - chunksOf - 10 - [ case M.findWithDefault 0 (V2 x y) m of - 1 -> '•' - _ -> ' ' - | x <- [(-5) .. 44] - , y <- [(-5) .. 4] - ] runIntcode :: (TuringMachine, RoboState) -> (TuringMachine, RoboState) runIntcode (tm, rb) =