-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path16a.hs
30 lines (24 loc) · 887 Bytes
/
16a.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
import AOC
main = interactg f
attribp :: String -> Int -> Bool
attribp s = or . mapM inRange rules
where
[_, rs] = splitOn ": " s
rules = map (map read . splitOn "-") $ splitOn " or " rs
inRange [low, high] x = low <= x && x <= high
attribp' :: String -> [Int]
attribp' s = concatMap valid rules
where
[_, rs] = splitOn ": " s
rules = map (map read . splitOn "-") $ splitOn " or " rs
valid [low, high] = [low..high]
ticketp :: String -> [Int]
ticketp = map read . splitOn ","
f [as, [_, t], _:ts] = sum $ filter matchesNoRules $ concat tickets
where
(attribs, tickets) = (map attribp as, map ticketp ts)
matchesNoRules = not . or . sequence attribs
f' [as, [_, t], _:ts] = sum $ filter (`notElem` validNums) $ concat tickets
where
(attribs, _, tickets) = (map attribp' as, ticketp t, map ticketp ts)
validNums = nub $ concat attribs