do not enumerate paths; only count
This commit is contained in:
parent
a8404b5872
commit
7c4c119eff
|
|
@ -16,16 +16,16 @@ addPath (from,to) = unionWith (++) (Data.Map.fromList [(from,[to]), (to,[from])]
|
|||
isUpper :: String -> Bool
|
||||
isUpper = Data.Char.isUpper . head
|
||||
|
||||
pathFinder :: System -> [String] -> HashSet String -> [[String]]
|
||||
pathFinder :: System -> [String] -> HashSet String -> Int
|
||||
pathFinder _ [] _ = error "no starting point!"
|
||||
pathFinder sys p@("end":xs) _ = [p]
|
||||
pathFinder sys p@(x:xs) visited = concatMap (\next -> pathFinder sys (next:p) (next `insert` visited)) elig
|
||||
pathFinder sys p@("end":xs) _ = 1
|
||||
pathFinder sys p@(x:xs) visited = sum $ map (\next -> pathFinder sys (next:p) (next `insert` visited)) elig
|
||||
where elig = filter (\cave -> isUpper cave || not (cave `member` visited)) (sys ! x)
|
||||
|
||||
pathFinder2 :: System -> [String] -> HashSet String -> [[String]]
|
||||
pathFinder2 :: System -> [String] -> HashSet String -> Int
|
||||
pathFinder2 _ [] _ = error "no starting point!"
|
||||
pathFinder2 sys p@("end":xs) _ = [p]
|
||||
pathFinder2 sys p@(x:xs) visited = concatMap (\next -> f next sys (next:p) (next `insert` visited)) elig
|
||||
pathFinder2 sys p@("end":xs) _ = 1
|
||||
pathFinder2 sys p@(x:xs) visited = sum $ map (\next -> f next sys (next:p) (next `insert` visited)) elig
|
||||
where
|
||||
f cave = if isUpper cave || not (cave `member` visited) then pathFinder2 else pathFinder
|
||||
elig = filter (/= "start") (sys ! x)
|
||||
|
|
@ -36,9 +36,7 @@ main = do
|
|||
let system = foldr addPath Data.Map.empty input
|
||||
|
||||
putStrLn "part 1: "
|
||||
let paths = map reverse $ pathFinder system ["start"] (Data.HashSet.fromList ["start"])
|
||||
print $ length paths
|
||||
print $ pathFinder system ["start"] (Data.HashSet.fromList ["start"])
|
||||
|
||||
putStrLn "part 2: "
|
||||
let paths = map reverse $ pathFinder2 system ["start"] (Data.HashSet.fromList ["start"])
|
||||
print $ length paths
|
||||
print $ pathFinder2 system ["start"] (Data.HashSet.fromList ["start"])
|
||||
|
|
|
|||
Loading…
Reference in New Issue