module Main where import System.IO import System.Environment import qualified Data.Map as Map import qualified Data.Set as Set import LeastReachableCity import Types {- format of input file is: - mileage - from to len - from to len - ... - where "from" and "to" are numbers of graph vertices, "len" is distance between them - -} main :: IO() main = do argv <- getArgs filename <- if not $ null argv then do head <$> getArgs else do putStrLn "Usage: ./lrc [filename]\nNow defaulting to file \"data\"" return "data" content <- readFile filename let mileage = read $ head $ lines content :: Len let graph = readGraph $ tail content let (city, n) = leastReachableCity graph mileage putStrLn $ "The least reachable city is " ++ (show city) ++ " (" ++ (show n) ++ " city/ies is/are reachable)" {- For parsing graph using content of the file -} parseStrNode :: [String] -> Node parseStrNode [a, b, c] = Node (p a, p b, l c) where p x = read x :: Point l x = read x :: Len readGraph :: String -> Graph readGraph content = parse lst where parse (x:xs) = if length ws == 3 then (parseStrNode ws) : (parse xs) else parse xs where ws = words x parse _ = [] lst = lines content