-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.hs
27 lines (23 loc) · 829 Bytes
/
day2.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
main = do
contents <- readFile "day2_input"
let input = lines contents
-- Part 1
let res = length $ filter (isValid . getEntry) input
print res
-- Part 2
let res = length $ filter (isValidSecond . getEntry) input
print res
isValid::(Int, Int, Char, String) -> Bool
isValid (low, high, char, str) =
low <= count && count <= high
where count = length $ filter (== char) str
isValidSecond::(Int, Int, Char, String) -> Bool
isValidSecond (low, high, char, str) =
left /= right
where left = str !! (low - 1) == char
right = str !! (high - 1) == char
getEntry::String -> (Int, Int, Char, String)
getEntry s =
let (min, _:rest) = break (=='-') s
(max, _:c:_:_:r) = break (==' ') rest
in ((read::String->Int) min, (read::String->Int) max, c, r)