-
Notifications
You must be signed in to change notification settings - Fork 3
/
Main.hs
55 lines (47 loc) · 1.39 KB
/
Main.hs
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
46
47
48
49
50
51
52
53
54
55
import Data.List (isPrefixOf)
import Data.List (foldl')
import Data.Text (Text, strip)
import Debug.Trace
import Lib.Inputs
import Lib.Strings
import System.IO
main :: IO ()
main = do
handle <- openFile "input.txt" ReadMode
contents <- hGetContents handle
let linesList = lines contents
print $ fn1 linesList
print $ fn2 linesList
-- print $ foldl (\acc a -> acc + (travelDistance a 10 10)) 0 [1 .. 10]
fn1 :: [String] -> Int
fn1 line = res times distances
where
times = parse1 (line !! 0)
distances = parse1 (line !! 1)
parse1 :: String -> [Int]
parse1 s = [read x :: Int | x <- getStrings del, x /= ""]
where
line = trimSpace $ substringAfter s ": "
del = newDelimiter line " "
res :: [Int] -> [Int] -> Int
res [] [] = 1
res (x:xs) (y:ys) = n * res xs ys
where
n = numberOfWays x y
numberOfWays :: Int -> Int -> Int
numberOfWays time distance = foldl' (\acc i -> acc + (travelDistance i time distance)) 0 [1 .. (time - 1)]
travelDistance :: Int -> Int -> Int -> Int
travelDistance hold raceTime distance =
if (raceTime - hold) * hold > distance
then 1
else 0
fn2 :: [String] -> Int
fn2 line = numberOfWays time distance
where
time = parse2 (line !! 0)
distance = parse2 (line !! 1)
parse2 :: String -> Int
parse2 s = read (concat [x | x <- getStrings del, x /= ""]) :: Int
where
line = trimSpace $ substringAfter s ": "
del = newDelimiter line " "