Rename for better sorting on github

This commit is contained in:
2023-12-10 14:03:24 +00:00
parent 2e2bea3cff
commit 27a935933c
43 changed files with 9 additions and 9 deletions

2
day06/eg.txt Normal file
View File

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

2
day06/input.txt Normal file
View File

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

38
day06/solution.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