{-# Language QuasiQuotes #-}
module Main where
import Advent (format)
import Data.Graph.Inductive (UGr, reachable, noComponents, mkUGraph)
main :: IO ()
IO ()
main =
do input <- [format|2017 12 (%u <-> %u&(, )%n)*|]
let g = [(Node, [Node])] -> UGr
toGraph [(Node, [Node])]
input
print (length (reachable 0 g))
print (noComponents g)
toGraph :: [(Int,[Int])] -> UGr
toGraph :: [(Node, [Node])] -> UGr
toGraph [(Node, [Node])]
xs = [Node] -> [Edge] -> UGr
forall (gr :: * -> * -> *).
Graph gr =>
[Node] -> [Edge] -> gr () ()
mkUGraph ((Node, [Node]) -> Node
forall a b. (a, b) -> a
fst ((Node, [Node]) -> Node) -> [(Node, [Node])] -> [Node]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(Node, [Node])]
xs) ((Node, [Node]) -> [Edge]
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
forall (f :: * -> *) a. Applicative f => (Node, f a) -> f (Node, a)
sequenceA ((Node, [Node]) -> [Edge]) -> [(Node, [Node])] -> [Edge]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< [(Node, [Node])]
xs)