blob: 1346a688230ceb32f1112d49f71ae794a1d6462c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
module Day13A where
import System.Environment (getArgs)
import Data.Maybe (catMaybes)
import Data.List.Split (splitOn)
import Text.Read (readMaybe)
import Text.Printf
-- Get the nearest bus in the list after the given time
-- Returns (time, bus)
getNearest :: Int -> [Int] -> (Int, Int)
getNearest e xs | not $ null rs = (e, head rs)
| otherwise = getNearest (e + 1) xs
where rs = filter (\x -> e `mod` x == 0) xs
-- Parse the input as a string
-- Returns (current time, list of valid buses)
parseInput :: String -> (Int, [Int])
parseInput x = (read t, catMaybes ts)
where (t:r) = lines x
bs = splitOn "," (head r)
ts :: [Maybe Int]
ts = map readMaybe bs
-- Parse a file given the path
-- Returns (current time, list of valid buses)
parseFromFile :: String -> IO (Int, [Int])
parseFromFile s = do
contents <- readFile s;
return $ parseInput contents;
-- runghc --ghc-arg='-package split' 13a.hs inputs/day13
main :: IO ()
main = do
args <- getArgs;
(now, bs) <- parseFromFile (head args);
let (t, b) = getNearest now bs;
let w = t - now;
printf "You could get bus %d at %d\n" b t :: IO ();
printf "This is a %d minute wait\n" w :: IO ();
printf "Answer = %d\n" (b * w) :: IO ();
return ();
|