AoC/2019/day13/Helpers.hs
2019-12-13 19:19:55 +01:00

46 lines
1.1 KiB
Haskell

module Helpers
( v2x
, v2y
, drawMap
, readIntcode
) where
import Data.List
import Data.List.Split
import qualified Data.Map as M
import qualified Data.Vector as V
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 $
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
readIntcode :: String -> V.Vector Integer
readIntcode = V.fromList . concatMap (map read . splitOn ",") . lines