-
Notifications
You must be signed in to change notification settings - Fork 0
/
d4.hs
36 lines (29 loc) · 992 Bytes
/
d4.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
32
33
34
35
36
import Data.Maybe (fromJust)
import Data.List (elemIndex)
splitAt' :: Int -> String -> (String, String)
splitAt' i s = (take i s, drop (i+1) s)
parse :: String -> ((Int, Int), (Int, Int))
parse s = (parse' p1, parse' p2) where
piv = fromJust $ elemIndex ',' s
(p1, p2) = splitAt' piv s
parse' :: String -> (Int, Int)
parse' s' = (read left, read right) where
piv = fromJust $ elemIndex '-' s'
(left, right) = splitAt' piv s'
f1 :: ((Int, Int), (Int, Int)) -> Bool
f1 ((a1, b1), (a2, b2)) = p1 || p2 where
p1 = (a1 <= a2) && (b1 >= b2)
p2 = (a2 <= a1) && (b2 >= b1)
f2 :: ((Int, Int), (Int, Int)) -> Bool
f2 ((a1, b1), (a2, b2)) = p1 || p2 where
p1 = (a2 <= a1) && (a1 <= b2)
p2 = (a1 <= a2) && (a2 <= b1)
task1 :: String -> Int
task1 s = (length.filter id) $ map (f1.parse) $ lines s
task2 :: String -> Int
task2 s = (length.filter id) $ map (f2.parse) $ lines s
main :: IO ()
main = do
input <- readFile "d4.txt"
print $ task1 input
print $ task2 input