-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday05.hs
More file actions
29 lines (20 loc) · 743 Bytes
/
Copy pathday05.hs
File metadata and controls
29 lines (20 loc) · 743 Bytes
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
#!/usr/bin/env runghc
module Main where
import Data.List (sortOn)
import Data.List.Split (splitOn)
type Range = (Int, Int)
main = interact (unlines . sequence [part1, part2] . parse)
part1 = ("Part 1: " ++) . show . length . filter id . uncurry (map . inRanges)
part2 = ("Part 2: " ++) . show . count . fst
count = snd . foldl go (0, 0) . sortOn fst
where
go (ptr, acc) (l, u)
| u > ptr = (u, acc + u - (ptr + 1) `max` l + 1)
| otherwise = (ptr, acc)
inRanges rr v = any (v `inRange`) rr
inRange v (l, u) = l <= v && v <= u
parse :: String -> ([(Int, Int)], [Int])
parse = parts . map lines . splitOn "\n\n"
where
parts [r, a] = (map range r, map read a)
range = (\[l, u] -> (read l, read u)) . splitOn "-"