{-# Language QuasiQuotes #-}
module Main (main) where
import Advent.Format ( format )
import Data.Graph.Inductive.Graph (mkUGraph)
import Data.Graph.Inductive.PatriciaTree (UGr)
import Data.Graph.Inductive.Query (noComponents)
main :: IO ()
IO ()
main =
do [[Int]]
input <- [format|2018 25 (%d&,%n)*|]
Int -> IO ()
forall a. Show a => a -> IO ()
print (Gr () () -> Int
forall (gr :: * -> * -> *) a b. Graph gr => gr a b -> Int
noComponents ([[Int]] -> Gr () ()
starGraph [[Int]]
input))
starGraph :: [[Int]] -> UGr
starGraph :: [[Int]] -> Gr () ()
starGraph [[Int]]
stars =
[Int] -> [Edge] -> Gr () ()
forall (gr :: * -> * -> *). Graph gr => [Int] -> [Edge] -> gr () ()
mkUGraph [ Int
1 .. [[Int]] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [[Int]]
stars ]
[ (Int
i,Int
j) | (Int
i,[Int]
x) <- [Int] -> [[Int]] -> [(Int, [Int])]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
1..] [[Int]]
stars
, (Int
j,[Int]
y) <- [Int] -> [[Int]] -> [(Int, [Int])]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
1..] [[Int]]
stars
, [Int] -> [Int] -> Int
manhattan [Int]
x [Int]
y Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
3 ]
manhattan :: [Int] -> [Int] -> Int
manhattan :: [Int] -> [Int] -> Int
manhattan [Int]
x [Int]
y = [Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ((Int -> Int -> Int) -> [Int] -> [Int] -> [Int]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\Int
a Int
b -> Int -> Int
forall a. Num a => a -> a
abs (Int
aInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
b)) [Int]
x [Int]
y)