2019-12-13 11:34:11 +01:00
module Helpers
( v2x
, v2y
, drawMap
2019-12-13 19:19:55 +01:00
, readIntcode
2019-12-13 11:34:11 +01:00
) where
import Data.List
import Data.List.Split
import qualified Data.Map as M
2019-12-13 19:19:55 +01:00
import qualified Data.Vector as V
2019-12-13 11:34:11 +01:00
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
2019-12-13 19:19:55 +01:00
readIntcode :: String -> V . Vector Integer
readIntcode = V . fromList . concatMap ( map read . splitOn " , " ) . lines