You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

47 lines
1.3 KiB
Haskell

2 years ago
module Main where
import System.IO
import System.Environment
2 years ago
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"
2 years ago
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)"
2 years ago
{- 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