-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day25.hs
43 lines (36 loc) · 1.07 KB
/
Day25.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
module Javran.AdventOfCode.Y2018.Day25 (
) where
import Control.Monad
import Control.Monad.ST
import qualified Data.Vector as V
import Javran.AdventOfCode.Prelude
import qualified Javran.AdventOfCode.UnionFind.ST as UF
import Text.ParserCombinators.ReadP hiding (count, get, many)
data Day25 deriving (Generic)
type Pt = (Int, Int, Int, Int)
ptP :: ReadP Pt
ptP = do
[a, b, c, d] <- intP `sepBy1` char ','
pure (a, b, c, d)
where
intP = readS_to_P (reads @Int)
countConstellations :: [Pt] -> ST s Int
countConstellations xs = do
let pts = V.fromList xs
ss <- V.fromListN (V.length pts) <$> mapM UF.fresh xs
forM_
( do
(i, xs0) <- pickInOrder [0 .. V.length pts - 1]
j <- xs0
pure (i, j)
)
$ \(i, j) -> do
when
(manhattan (pts V.! i) (pts V.! j) <= 3)
do
UF.union (ss V.! i) (ss V.! j)
UF.countClusters ss
instance Solution Day25 where
solutionRun _ SolutionContext {getInputS, answerShow} = do
xs <- fmap (consumeOrDie ptP) . lines <$> getInputS
answerShow $ runST $ countConstellations xs