From c8b7557a0605d8a7c46abfab66293e105d9f8aa1 Mon Sep 17 00:00:00 2001 From: Quinten Kock Date: Sun, 12 Dec 2021 06:58:31 +0100 Subject: [PATCH] Clean up day12p2 --- 2021/day12.hs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/2021/day12.hs b/2021/day12.hs index d213652..03d61c3 100644 --- a/2021/day12.hs +++ b/2021/day12.hs @@ -1,9 +1,7 @@ module Day12 where -import qualified Data.HashSet -import Data.Map ( Map, fromList, unionWith, empty, toList, (!) ) +import Data.Map ( Map, fromList, unionWith, empty, (!) ) import Data.List.Split (splitOn) import qualified Data.Char -import Data.Maybe (mapMaybe) type Path = (String,String) type System = Map String [String] @@ -24,16 +22,12 @@ pathFinder sys p@("end":xs) = [p] pathFinder sys p@(x:xs) = concatMap (\next -> pathFinder sys (next:p)) elig where elig = filter (\cave -> isUpper cave || cave `notElem` p) (sys ! x) -pathFinder2 :: System -> [String] -> Bool -> [[String]] -pathFinder2 sys p@("end":xs) _ = [p] -pathFinder2 sys p@(x:xs) True = concatMap (\next -> pathFinder2 sys (next:p) True) elig - where elig = filter (\cave -> isUpper cave || cave `notElem` p) (sys ! x) -pathFinder2 sys p@(x:xs) False = concatMap (\next -> pathFinder2 sys (next:p) (dup next)) elig +pathFinder2 :: System -> [String] -> [[String]] +pathFinder2 sys p@("end":xs) = [p] +pathFinder2 sys p@(x:xs) = concatMap (\next -> f next sys (next:p)) elig where + f cave = if isUpper cave || cave `notElem` p then pathFinder2 else pathFinder elig = filter (/= "start") (sys ! x) - dup cave = isLower cave && cave `elem` p - - main :: IO () main = do @@ -45,5 +39,5 @@ main = do print $ length paths putStrLn "part 2: " - let paths = map reverse $ pathFinder2 system ["start"] False + let paths = map reverse $ pathFinder2 system ["start"] print $ length paths \ No newline at end of file