···
pairs xs = [(x, y) | (x:ys) <- tails xs, y <- ys]
24
-
lastParent :: (Ord a, Show a) => a -> Map a a -> a
26
-
case Map.lookup a m of
27
-
Just a' -> lastParent a' m
24
+
merge :: (Point3, Point3) -> Map Point3 Int -> Map Point3 Int
28
+
in if pid == qid then
31
+
Map.map (\c -> if c == pid then qid else c) m
part1 :: String -> String
···
dropWhile (\(p,q) -> p == q) $
sortBy (comparing (\(p,q) -> dist2 p q)) $
38
-
foldl (\parents' (a,b) -> let a' = lastParent a parents'
39
-
b' = lastParent b parents'
43
-
Map.insert b' a' parents')
41
+
foldl (\ids' edge -> merge edge ids')
42
+
(Map.fromList $ zip boxes [1..])
(take 1000 connectionOrder)
51
-
foldl (\sizes' b -> Map.insertWith (+) (lastParent b parents) 1 sizes')
49
+
Map.foldl (\sizes' c -> Map.insertWith (+) c 1 sizes')
in show $ product $ take 3 sizes
part2 :: String -> String
···
dropWhile (\(p,q) -> p == q) $
sortBy (comparing (\(p,q) -> dist2 p q)) $
64
-
foldl (\parents' (a,b) -> let a' = lastParent a parents'
65
-
b' = lastParent b parents'
69
-
Map.insert b' a' parents')
71
-
(take 4791 connectionOrder)
73
-
foldl (\sizes' b -> Map.insertWith (+) (lastParent b parents) 1 sizes')
76
-
in show $ (connectionOrder !! 4791)
62
+
scanl (\ids' edge -> merge edge ids')
63
+
(Map.fromList $ zip boxes [1..])
66
+
filter (\((p,q),ids) -> ids Map.! p /= ids Map.! q)
67
+
$ zip connectionOrder circuits
68
+
((p,q),_) = merges !! (length boxes - 2)