Day 11: Find bounds, print map, move to Helpers

This commit is contained in:
shu 2019-12-13 10:45:20 +01:00
parent b8e7d09015
commit 3cf5c0179d
2 changed files with 43 additions and 16 deletions

41
2019/day11/Helpers.hs Normal file
View File

@ -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

View File

@ -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) =