-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day10.hs
46 lines (40 loc) · 1.54 KB
/
Day10.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
-- |
-- Module: Day10
-- Description: <https://adventofcode.com/2024/day/10 Day 10: Hoof It>
module Day10 (part1, part2) where
import Data.Char (digitToInt, isDigit)
import Data.IntMap qualified as IntMap (findWithDefault, fromListWith)
import Data.List.NonEmpty (NonEmpty ((:|)))
import Data.Map (Map)
import Data.Map qualified as Map (elems, fromListWith, fromSet, toList)
import Data.Monoid (Sum (Sum, getSum))
import Data.Set (Set)
import Data.Set qualified as Set (empty, member, singleton, size)
import Data.Text (Text)
import Data.Text qualified as T (lines, unpack)
parse :: Text -> NonEmpty (Set (Int, Int))
parse input = IntMap.findWithDefault Set.empty `flip` elevations <$> 0 :| [1 .. 9]
where
elevations =
IntMap.fromListWith (<>) $
[ (digitToInt c, Set.singleton (y, x))
| (y, line) <- zip [0 ..] $ T.lines input,
(x, c) <- zip [0 ..] $ T.unpack line,
isDigit c
]
adj :: (Int, Int) -> [(Int, Int)]
adj (y, x) = [(y - 1, x), (y, x - 1), (y, x + 1), (y + 1, x)]
bfs :: (Semigroup a) => ((Int, Int) -> a) -> NonEmpty (Set (Int, Int)) -> Map (Int, Int) a
bfs start (zero :| elevations) = foldl bfs' (Map.fromSet start zero) elevations
where
bfs' acc points =
Map.fromListWith (<>) $
[ (q, m)
| (p, m) <- Map.toList acc,
q <- adj p,
q `Set.member` points
]
part1 :: Text -> Int
part1 input = sum $ Set.size <$> bfs Set.singleton (parse input)
part2 :: Text -> Int
part2 input = getSum $ mconcat $ Map.elems $ bfs (const $ Sum 1) (parse input)