Add day 4
This commit is contained in:
parent
8519e093ea
commit
d3641e80be
28
4.hs
Normal file
28
4.hs
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
{-# LANGUAGE ScopedTypeVariables #-}
|
||||||
|
import Data.Char
|
||||||
|
import Data.List.Split
|
||||||
|
import Data.List
|
||||||
|
import Data.Maybe
|
||||||
|
|
||||||
|
main = do
|
||||||
|
ls <- readFile "4.in"
|
||||||
|
putStrLn . show . solveB . parse$ ls
|
||||||
|
where
|
||||||
|
solveA :: [[[Int]]] -> Int
|
||||||
|
solveA = length . filter id . map contains
|
||||||
|
|
||||||
|
solveB :: [[[Int]]] -> Int
|
||||||
|
solveB = length . filter id . map overlaps
|
||||||
|
|
||||||
|
parse :: String -> [[[Int]]]
|
||||||
|
parse = map ( map (map parseInt . splitOn "-") . splitOn ",") .lines where
|
||||||
|
parseInt = read :: String -> Int
|
||||||
|
|
||||||
|
contains :: Ord a => [[a]] -> Bool
|
||||||
|
contains [[a,b],[c,d]] = isSorted [a, c, d, b] || isSorted [c, a, b, d]
|
||||||
|
|
||||||
|
overlaps :: Ord a => [[a]] -> Bool
|
||||||
|
overlaps [[a,b],[c,d]] = isSorted [a,c,b] || isSorted [a,d,b] || isSorted [c, a, d] || isSorted [c, b, d]
|
||||||
|
|
||||||
|
isSorted :: Ord a => [a] -> Bool
|
||||||
|
isSorted x = x == sort x
|
Loading…
Reference in New Issue
Block a user