From 2bf34f458287b5b5b82eed4fd0b088c8ede9cfe5 Mon Sep 17 00:00:00 2001 From: Amneesh Singh Date: Tue, 10 Dec 2024 18:10:18 +0530 Subject: [PATCH] day 10: remove directions Signed-off-by: Amneesh Singh --- src/Day10.hs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/src/Day10.hs b/src/Day10.hs index 6aee65c..602995e 100644 --- a/src/Day10.hs +++ b/src/Day10.hs @@ -8,25 +8,16 @@ type Coord = (Int, Int) type Grid = M.Map (Int, Int) Int -data Dir = North | South | East | West deriving (Show) - -next :: Coord -> Dir -> Coord -next (x, y) North = (x, y - 1) -next (x, y) South = (x, y + 1) -next (x, y) East = (x + 1, y) -next (x, y) West = (x - 1, y) - part1 :: Grid -> Int part1 grid = sum . map (S.size . go 0 S.empty) . M.keys . M.filter (== 0) $ grid where go :: Int -> S.Set Coord -> Coord -> S.Set Coord - go h s c + go h s c@(x, y) | h == 9 = S.insert c s | otherwise = S.unions [ go h' s n - | d <- [North, South, East, West], - let n = next c d, + | n <- [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)], Just h' <- [M.lookup n grid], h' == h + 1 ] @@ -35,13 +26,12 @@ part2 :: Grid -> Int part2 grid = sum . map (go 0) . M.keys . M.filter (== 0) $ grid where go :: Int -> Coord -> Int - go h c + go h (x, y) | h == 9 = 1 | otherwise = sum [ go h' n - | d <- [North, South, East, West], - let n = next c d, + | n <- [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)], Just h' <- [M.lookup n grid], h' == h + 1 ]