This commit is contained in:
Gleb Koval 2023-12-06 14:18:27 +00:00
parent ea5c6c1e3b
commit 6863732c5a
Signed by: cyclane
GPG Key ID: 15E168A8B332382C
4 changed files with 47 additions and 1 deletions

View File

@ -4,3 +4,7 @@ root = true
end_of_line = lf
insert_final_newline = false
trim_trailing_whitespace = true
[*.hs]
indent_size = 2
indent_style = space

2
day6/eg.txt Normal file
View File

@ -0,0 +1,2 @@
Time: 7 15 30
Distance: 9 40 200

2
day6/input.txt Normal file
View File

@ -0,0 +1,2 @@
Time: 56 71 79 99
Distance: 334 1135 1350 2430

38
day6/main.hs Normal file
View File

@ -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