Advertisement
Guest User

Untitled

a guest
Dec 13th, 2021
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.Map
  2. import Data.Char
  3. import Text.Regex.Posix
  4.  
  5. cRoutes g [] _ _ _ = []
  6. cRoutes g (n:ns) p v rc
  7.     | n == "end"       = [p ++ ["end"]] ++ cRoutes g ns p v rc
  8.     | isUpper (n !! 0) = cRoutes g (g ! n) (p++[n]) v rc ++ cRoutes g ns p v rc
  9.     | notElem n v      = cRoutes g (g ! n) (p++[n]) (n:v) rc ++ cRoutes g ns p v rc
  10.     | rc == "" && n /= "start"  = cRoutes g (g ! n) (p++[n]) v n ++ cRoutes g ns p v rc
  11.     | otherwise        = cRoutes g ns p v rc
  12.  
  13. generateGraph [] m = m
  14. generateGraph (g:gs) m = let [[_,from,to]] = g =~ "(.*)-(.*)" :: [[String]]
  15.                          in generateGraph gs $ insertWith (++) from [to] $ insertWith (++) to [from] m
  16.  
  17. main = do fileContent <- readFile "day12.in"
  18.           let linesOfFile = lines fileContent
  19.               graph = generateGraph linesOfFile Data.Map.empty
  20.           print $ length $ cRoutes graph ["start"] [] [] "."
  21.           print $ length $ cRoutes graph ["start"] [] [] ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement