AoC/2022/day15/day15.hs
Gattix 44730f650f day 15: extra naive solution
too slow ┐( ̄ー ̄)┌
2022-12-30 15:11:20 +01:00

63 lines
1.5 KiB
Haskell

import Data.List.Split
import qualified Data.Map as M
import Control.Lens
import Data.List
import Linear.V2
type Sensor = (V2 Int, V2 Int)
type Cave = M.Map (V2 Int) Char
main :: IO ()
main = do
input <- map (splitOneOf "=,:") . lines <$> readFile "input"
let p = map parse input
let c = M.fromList $ fillCave p
putStrLn $ intercalate "\n" $ print2D 50 $ map fst $ M.toList c
let f = fillCave2 p
print $ M.filterWithKey (\(V2 _ a) b -> a == 10) f
print $ length $ M.filterWithKey (\(V2 _ a) b -> a == 10 && b == '#') $ M.union f c
parse :: [String] -> Sensor
parse [_, a, _, b, _, c, _, d] = (V2 (read a) (read b), V2 (read c) (read d))
dist :: Sensor -> Int
dist (a, b) = sum $ abs $ a - b
fillCave :: [Sensor] -> [(V2 Int, Char)]
fillCave (x:xs) = sensor ++ fillCave xs
where d = dist x
pRel = [V2 x y | x<-[(-d)..(d)],y<-[(-d)..(d)], abs x + abs y <= d]
sensor = zip (map (+fst x) pRel) (repeat '#')
fillCave [] = []
fillCave2 :: [Sensor] -> Cave
fillCave2 (x:xs) = newC `M.union` fillCave2 xs
where newC = M.insert (snd x) 'B' $ M.insert (fst x) 'S' (M.empty)
fillCave2 [] = M.empty
p :: Int -> [Int] -> String
p n (x:xs) =
if n == x
then '#' : p (n + 1) xs
else '.' : p (n + 1) (x : xs)
p _ [] = []
print2D :: Int -> [V2 Int] -> [String]
print2D n coords
| n == pred minY = []
| otherwise = p minX line : print2D (n - 1) coords
where
line = nub $ sort $ map (^. _x) (filter (\(V2 _ a) -> a == n) coords)
minY = minimum $ map (^. _y) coords
minX = minimum $ map (^. _x) coords