From 56f6951415a1be410da430096a20603b50bc9272 Mon Sep 17 00:00:00 2001 From: Amneesh Singh Date: Sun, 17 Dec 2023 21:42:23 +0530 Subject: [PATCH] day 11 Signed-off-by: Amneesh Singh --- day11.hs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 day11.hs diff --git a/day11.hs b/day11.hs new file mode 100644 index 0000000..7cf8e5d --- /dev/null +++ b/day11.hs @@ -0,0 +1,31 @@ +import Data.List (tails) + +type Coord = (Int, Int) + +-- slow but dont care +main :: IO () +main = do + input <- lines <$> readFile "day11.in" + + let galaxies = [(x, y) | (row, x) <- zip input [0 ..], ('#', y) <- zip row [0 ..]] + + let emptyRows = [r | r <- [0 .. length input], r `notElem` map fst galaxies] + let emptyCols = [c | c <- [0 .. length $ head input], c `notElem` map snd galaxies] + + let galaxyPairs = [(x, y) | (x : xs) <- tails galaxies, y <- xs] + + putStr "Q1: " + print . sum $ map (uncurry $ distance emptyRows emptyCols 2) galaxyPairs + + putStr "Q2: " + print . sum $ map (uncurry $ distance emptyRows emptyCols 1000000) galaxyPairs + +distance :: [Int] -> [Int] -> Int -> Coord -> Coord -> Int +distance rs cs f (x1, y1) (x2, y2) = + abs (x1 - x2) + + abs (y1 - y2) + + (f - 1) * length (filter (between x1 x2) rs) + + (f - 1) * length (filter (between y1 y2) cs) + +between :: Int -> Int -> Int -> Bool +between a b x = x > min a b && x < max a b