rewrite to not keep track of path
This commit is contained in:
parent
7c4c119eff
commit
8031b0327a
|
|
@ -4,8 +4,10 @@ import Data.Map ( Map, fromList, unionWith, empty, (!) )
|
||||||
import Data.List.Split (splitOn)
|
import Data.List.Split (splitOn)
|
||||||
import qualified Data.Char
|
import qualified Data.Char
|
||||||
|
|
||||||
type Path = (String,String)
|
type Cave = String
|
||||||
type System = Map String [String]
|
type Path = (Cave,Cave)
|
||||||
|
type System = Map Cave [Cave]
|
||||||
|
|
||||||
|
|
||||||
readPath :: String -> Path
|
readPath :: String -> Path
|
||||||
readPath s = (from,to) where [from,to] = splitOn "-" s
|
readPath s = (from,to) where [from,to] = splitOn "-" s
|
||||||
|
|
@ -13,19 +15,17 @@ readPath s = (from,to) where [from,to] = splitOn "-" s
|
||||||
addPath :: Path -> System -> System
|
addPath :: Path -> System -> System
|
||||||
addPath (from,to) = unionWith (++) (Data.Map.fromList [(from,[to]), (to,[from])])
|
addPath (from,to) = unionWith (++) (Data.Map.fromList [(from,[to]), (to,[from])])
|
||||||
|
|
||||||
isUpper :: String -> Bool
|
isUpper :: Cave -> Bool
|
||||||
isUpper = Data.Char.isUpper . head
|
isUpper = Data.Char.isUpper . head
|
||||||
|
|
||||||
pathFinder :: System -> [String] -> HashSet String -> Int
|
pathFinder :: System -> Cave -> HashSet Cave -> Int
|
||||||
pathFinder _ [] _ = error "no starting point!"
|
pathFinder sys "end" _ = 1
|
||||||
pathFinder sys p@("end":xs) _ = 1
|
pathFinder sys x visited = sum $ map (\next -> pathFinder sys next (next `insert` visited)) elig
|
||||||
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)
|
where elig = filter (\cave -> isUpper cave || not (cave `member` visited)) (sys ! x)
|
||||||
|
|
||||||
pathFinder2 :: System -> [String] -> HashSet String -> Int
|
pathFinder2 :: System -> Cave -> HashSet Cave -> Int
|
||||||
pathFinder2 _ [] _ = error "no starting point!"
|
pathFinder2 sys "end" _ = 1
|
||||||
pathFinder2 sys p@("end":xs) _ = 1
|
pathFinder2 sys x visited = sum $ map (\next -> f next sys next (next `insert` visited)) elig
|
||||||
pathFinder2 sys p@(x:xs) visited = sum $ map (\next -> f next sys (next:p) (next `insert` visited)) elig
|
|
||||||
where
|
where
|
||||||
f cave = if isUpper cave || not (cave `member` visited) then pathFinder2 else pathFinder
|
f cave = if isUpper cave || not (cave `member` visited) then pathFinder2 else pathFinder
|
||||||
elig = filter (/= "start") (sys ! x)
|
elig = filter (/= "start") (sys ! x)
|
||||||
|
|
@ -36,7 +36,7 @@ main = do
|
||||||
let system = foldr addPath Data.Map.empty input
|
let system = foldr addPath Data.Map.empty input
|
||||||
|
|
||||||
putStrLn "part 1: "
|
putStrLn "part 1: "
|
||||||
print $ pathFinder system ["start"] (Data.HashSet.fromList ["start"])
|
print $ pathFinder system "start" (Data.HashSet.fromList ["start"])
|
||||||
|
|
||||||
putStrLn "part 2: "
|
putStrLn "part 2: "
|
||||||
print $ pathFinder2 system ["start"] (Data.HashSet.fromList ["start"])
|
print $ pathFinder2 system "start" (Data.HashSet.fromList ["start"])
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue