Day 6
This commit is contained in:
parent
ea5c6c1e3b
commit
6863732c5a
|
@ -3,4 +3,8 @@ root = true
|
|||
[*]
|
||||
end_of_line = lf
|
||||
insert_final_newline = false
|
||||
trim_trailing_whitespace = true
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
[*.hs]
|
||||
indent_size = 2
|
||||
indent_style = space
|
|
@ -0,0 +1,2 @@
|
|||
Time: 7 15 30
|
||||
Distance: 9 40 200
|
|
@ -0,0 +1,2 @@
|
|||
Time: 56 71 79 99
|
||||
Distance: 334 1135 1350 2430
|
|
@ -0,0 +1,38 @@
|
|||
type Race = (Int, Int)
|
||||
|
||||
loadRaces :: String -> [Race]
|
||||
loadRaces str = zip ts ds
|
||||
where
|
||||
[ts, ds] = map getNs $ splitStr '\n' str
|
||||
getNs :: String -> [Int]
|
||||
getNs = map read . filter (not . null) . splitStr ' ' . drop 2 . dropWhile (/=':')
|
||||
|
||||
loadRace :: String -> Race
|
||||
loadRace str = (t, d)
|
||||
where
|
||||
[t, d] = map getN $ splitStr '\n' str
|
||||
getN :: String -> Int
|
||||
getN = read . filter (/= ' ') . drop 2 . dropWhile (/= ':')
|
||||
|
||||
splitStr :: Char -> String -> [String]
|
||||
splitStr onC = foldr splitStr' [""]
|
||||
where
|
||||
splitStr' :: Char -> [String] -> [String]
|
||||
splitStr' c (cur:splits)
|
||||
| c == onC = "":cur:splits
|
||||
| otherwise = (c:cur):splits
|
||||
|
||||
raceOptions :: Race -> Int
|
||||
raceOptions (t, d) = t + 1 - 2 * head [n | n <- [0..t], n * (t-n) > d]
|
||||
|
||||
part1 :: String -> String
|
||||
part1 = show . product . map raceOptions . loadRaces
|
||||
|
||||
part2 :: String -> String
|
||||
part2 = show . raceOptions . loadRace
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
s <- readFile "input.txt"
|
||||
putStrLn $ "Part 1: " ++ part1 s
|
||||
putStrLn $ "Part 2: " ++ part2 s
|
Loading…
Reference in New Issue