From f68695010643832645943cb49cd3f6503fc78c51 Mon Sep 17 00:00:00 2001 From: Gattix Date: Thu, 5 Jan 2023 15:06:45 +0100 Subject: [PATCH] d15p2 --- 2022/day15/day15.hs | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/2022/day15/day15.hs b/2022/day15/day15.hs index 58067ef..2129912 100644 --- a/2022/day15/day15.hs +++ b/2022/day15/day15.hs @@ -1,32 +1,24 @@ 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 + print $ "Cave from to: " ++ show (caveMinMaxX sensors) + print $ part1 (caveMinMaxX sensors) sensors + print $ part2 sensors 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 +getBorder (s:ss) = map (+ fst s) xs ++ getBorder ss + where + xs = + concat $ take 4 $ iterate (map perp) [V2 x (d - x) | x <- [0 .. pred d]] + d = succ $ dist s +getBorder [] = [] caveMinMaxX :: [Sensor] -> (Int, Int) caveMinMaxX sensors = (minimum k, maximum l) @@ -41,6 +33,16 @@ part1 (a, b) sensors = length $ filter (== '#') $ map (checkPoint sensors) xs where xs = [V2 x 2000000 | x <- [a .. b]] --y=10 for testinput +part2 :: [Sensor] -> Int +part2 sensors = + f $ + head $ + filter (\x -> checkPoint sensors x == ' ') $ + filter (\(V2 x y) -> x >= 0 && x <= 4000000 && y >= 0 && y <= 4000000) $ + getBorder sensors -- <=20 for testinput + where + f = (+) <$> view _y <*> ((*) 4000000 . view _x) + parse :: [String] -> Sensor parse [_, a, _, b, _, c, _, d] = (V2 (read a) (read b), V2 (read c) (read d))