-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay13.hs
31 lines (26 loc) · 963 Bytes
/
Day13.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
module Day13
( part1
, part2
) where
import Data.Bifunctor (second)
import Data.List (minimumBy)
import Data.List.Split (splitOn)
import Data.Maybe (fromJust)
import Math.NumberTheory.Moduli.Chinese (chinese)
findBus :: [[Int]] -> Int
findBus [[timestamp], timetable] =
(\x -> x * (x - mod timestamp x)) .
minimumBy (\a b -> compare (a - mod timestamp a) (b - mod timestamp b)) $
timetable
findTimeStamp :: [(Integer, Integer)] -> Integer
findTimeStamp (h:ead) =
(\(a, b) -> mod (b - a) b) . foldl (\a -> fromJust . chinese a) h $ ead
part1 :: Bool -> String -> String
part1 _ =
show . findBus . map (map read . filter (/= "x") . splitOn ",") . lines
part2 :: Bool -> String -> String
part2 _ =
show .
findTimeStamp .
map (second read) .
filter ((/= "x") . snd) . zip [0 ..] . splitOn "," . last . lines