day4
This commit is contained in:
		
							
								
								
									
										31
									
								
								day4/main.hs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								day4/main.hs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,31 @@
 | 
			
		||||
import System.IO
 | 
			
		||||
 | 
			
		||||
import Data.List.Split
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
parseGrp :: String -> [(Int, Int)]
 | 
			
		||||
parseGrp = map ((\ [s,e] -> (read s :: Int,read e :: Int)) . splitOn "-") . splitOn ","
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
contained :: [(Int, Int)] -> Bool
 | 
			
		||||
contained [(s1,e1),(s2,e2)]
 | 
			
		||||
    | s1 <= s2 && e1 >= e2 = True
 | 
			
		||||
    | s1 >= s2 && e1 <= e2 = True
 | 
			
		||||
    | otherwise = False
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
overlapping :: [(Int, Int)] -> Bool
 | 
			
		||||
overlapping [(s1,e1),(s2,e2)]
 | 
			
		||||
    | e1 < s2 = False
 | 
			
		||||
    | s1 > e2 = False
 | 
			
		||||
    | otherwise = True
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
handler :: String -> String
 | 
			
		||||
handler s = (show $ length $ filter contained groups) ++ "\n" ++
 | 
			
		||||
            (show $ length $ filter overlapping groups) ++ "\n"
 | 
			
		||||
    where groups = map parseGrp $ lines s
 | 
			
		||||
 | 
			
		||||
main :: IO ()
 | 
			
		||||
main = do
 | 
			
		||||
    interact handler
 | 
			
		||||
		Reference in New Issue
	
	Block a user