-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05.hs
29 lines (20 loc) · 879 Bytes
/
day05.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
module Main where
import Data.Either (lefts, rights)
import Data.List (partition, sortBy)
import Data.List.Split (splitOn)
main = interact (unlines . sequence [part1, part2] . parse)
part1, part2 :: [Either [Int] [Int]] -> String
part1 = ("Part 1: " ++) . show . sum . map mid . lefts
part2 = ("Part 2: " ++) . show . sum . map mid . rights
parts rules updates = zipWith cmp (map (sortBy rules) updates) updates
where
cmp sorted updates
| sorted == updates = Left updates
| otherwise = Right sorted
mid xs = xs !! (length xs `div` 2)
sorter :: (Ord a) => [(a, a)] -> a -> a -> Ordering
sorter rules a b = if (a, b) `elem` rules then LT else GT
parse = (\[r, u] -> parts (rules r) (updates u)) . map lines . splitOn "\n\n"
where
rules = sorter . map ((\[a,b] -> (read a, read b)) . splitOn "|")
updates = map (map read . splitOn ",")