AoC/2022/day15/day15.hs
2023-01-05 13:58:32 +01:00

61 lines
1.6 KiB
Haskell

import Control.Lens
import Data.List
import Data.List.Split
import qualified Data.Map as M
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 sensors = map parse input
-- print $ "Cave from to: " ++ show (caveMinMaxX sensors)
-- print $ part1 (caveMinMaxX sensors) sensors
-- print $ getBorder sensors
-- print $ last [(x,x) | x<-[0..10000]]
-- print $ last [(x,y) | x<-[0..10000], y<-[0..10000], x==y]
print "hello world"
allCandidates :: [Sensor] -> [Int] -> [V2 Int]
allCandidates (s:ss) ds = undefined
getBorder :: [Sensor] -> [V2 Int]
getBorder (s:ss) = map (+ (fst s)) xs ++ getBorder ss
where xs = [V2 x (d - x) | x<-[-d..0]++[1..d]]
d = succ $ dist s
caveMinMaxX :: [Sensor] -> (Int, Int)
caveMinMaxX sensors = (minimum k, maximum l)
where
d = map dist sensors
s = map (view _y . fst) sensors
k = zipWith (-) s d
l = zipWith (+) s d
part1 :: (Int, Int) -> [Sensor] -> Int
part1 (a, b) sensors = length $ filter (== '#') $ map (checkPoint sensors) xs
where
xs = [V2 x 2000000 | x <- [a .. b]] --y=10 for testinput
parse :: [String] -> Sensor
parse [_, a, _, b, _, c, _, d] = (V2 (read a) (read b), V2 (read c) (read d))
checkPoint :: [Sensor] -> V2 Int -> Char
checkPoint sensors point
| point `elem` map fst sensors = 'S'
| point `elem` map snd sensors = 'B'
| otherwise = checkPoint' sensors point
where
checkPoint' (s:ss) p =
if dist (fst s, p) <= dist s
then '#'
else checkPoint' ss p
checkPoint' [] _ = ' '
dist :: Sensor -> Int
dist (a, b) = sum $ abs $ a - b