From 871855ff85670fd9d4abe7c2168cdb8a6fd231f8 Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Sat, 16 Dec 2017 11:12:40 -0800 Subject: [PATCH] Day 12 - removed old solution in favour of graph-based solution --- 12.hs | 26 +++++--------------------- 12_graph.hs | 14 -------------- 2 files changed, 5 insertions(+), 35 deletions(-) delete mode 100644 12_graph.hs diff --git a/12.hs b/12.hs index 388fdfa..80d87c6 100644 --- a/12.hs +++ b/12.hs @@ -1,30 +1,14 @@ import Data.List.Split (splitOn) -import Data.IntMap (IntMap, findWithDefault, keys) -import Data.IntSet (IntSet, member, notMember, insert, delete, empty, size, findMin) -import qualified Data.IntMap as M (fromList) -import qualified Data.IntSet as S (fromList, null) +import Data.Graph (reachable, scc) +import Data.Array (array) parseLine :: String -> (Int, [Int]) parseLine str = let src : dests : [] = splitOn " <-> " str in (read src, map read $ splitOn ", " dests) -visit :: IntMap [Int] -> Int -> IntSet -> IntSet -visit hashmap node hashset = - let neighbours = filter (`notMember` hashset) $ findWithDefault [] node hashmap - in foldr (visit hashmap) (foldr insert hashset neighbours) neighbours - -remove :: IntMap [Int] -> Int -> IntSet -> IntSet -remove hashmap node hashset = - let neighbours = filter (`member` hashset) $ findWithDefault [] node hashmap - in foldr (remove hashmap) (foldr delete hashset neighbours) neighbours - -countGroups :: IntMap [Int] -> Int -> IntSet -> Int -countGroups hashmap count hashset = if S.null hashset then count else - countGroups hashmap (count + 1) $ remove hashmap (findMin hashset) hashset - main :: IO () main = do - hashmap <- fmap (M.fromList . map parseLine . lines) $ readFile "12.txt" - print $ size $ visit hashmap 0 empty - print $ countGroups hashmap 0 . S.fromList . keys $ hashmap \ No newline at end of file + graph <- fmap (array (0, 1999) . map parseLine . lines) $ readFile "12.txt" + print $ length $ reachable graph 0 + print $ length $ scc graph \ No newline at end of file diff --git a/12_graph.hs b/12_graph.hs deleted file mode 100644 index 80d87c6..0000000 --- a/12_graph.hs +++ /dev/null @@ -1,14 +0,0 @@ -import Data.List.Split (splitOn) -import Data.Graph (reachable, scc) -import Data.Array (array) - -parseLine :: String -> (Int, [Int]) -parseLine str = - let src : dests : [] = splitOn " <-> " str - in (read src, map read $ splitOn ", " dests) - -main :: IO () -main = do - graph <- fmap (array (0, 1999) . map parseLine . lines) $ readFile "12.txt" - print $ length $ reachable graph 0 - print $ length $ scc graph \ No newline at end of file