AoC/2019/day17/Helpers.hs

51 lines
1.2 KiB
Haskell
Raw Normal View History

2019-12-17 22:34:14 +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 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