diff --git a/.editorconfig b/.editorconfig index b4f00a4..05b9a68 100644 --- a/.editorconfig +++ b/.editorconfig @@ -3,4 +3,8 @@ root = true [*] end_of_line = lf insert_final_newline = false -trim_trailing_whitespace = true \ No newline at end of file +trim_trailing_whitespace = true + +[*.hs] +indent_size = 2 +indent_style = space \ No newline at end of file diff --git a/day6/eg.txt b/day6/eg.txt new file mode 100644 index 0000000..b39f49d --- /dev/null +++ b/day6/eg.txt @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200 \ No newline at end of file diff --git a/day6/input.txt b/day6/input.txt new file mode 100644 index 0000000..5f9b975 --- /dev/null +++ b/day6/input.txt @@ -0,0 +1,2 @@ +Time: 56 71 79 99 +Distance: 334 1135 1350 2430 \ No newline at end of file diff --git a/day6/main.hs b/day6/main.hs new file mode 100644 index 0000000..6afc553 --- /dev/null +++ b/day6/main.hs @@ -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 \ No newline at end of file