day 10: remove directions

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2024-12-10 18:10:18 +05:30
parent b6cdbb6dd8
commit 2bf34f4582

View File

@@ -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
]