AoC/2019/day18/Helpers.hs

50 lines
1.1 KiB
Haskell
Raw Normal View History

2019-12-18 15:02:42 +01:00
module Helpers
( v2x
, v2y
, drawMap
, drawLine
, unitVecs
) 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
unitVecs :: [V2 Int]
unitVecs = take 4 $ iterate perp (V2 0 1)
drawLine :: V2 Int -> [V2 Int]
drawLine (V2 0 0) = []
drawLine (V2 a b)
| abs a > abs b = V2 (a - signum a) b : drawLine (V2 (a - signum a) b)
| otherwise = V2 a (b - signum b) : drawLine (V2 a (b - signum b))
--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
getFromMap f1 f2 = M.foldrWithKey (\k _ result -> f1 [f2 k, result]) 0 m
drawMap :: M.Map (V2 Int) Char -> String
drawMap m =
intercalate "\n" $
transpose $
chunksOf
(abs (abs y1 - abs y2) + 1)
[ M.findWithDefault ' ' (V2 x y) m
| x <- [x1 .. x2]
, y <- [y1 .. y2]
]
where
(x1, y1, x2, y2) = getBounds m