advent-of-code/app/four.hs

29 lines
861 B
Haskell
Raw Normal View History

2022-12-17 14:50:06 +01:00
import Control.Monad
import Data.Char
parseLine :: String -> [[Int]]
parseLine line = [parseAssignment $ takeWhile (/= ',') line,
parseAssignment $ drop 1 $ dropWhile (',' /= ) line]
parseAssignment :: String -> [Int]
parseAssignment assignment = do
let first = takeWhile ('-' /=) assignment
let second = drop 1 $ dropWhile ('-' /=) assignment
[(read first).. (read second)]
isIncluded :: [Int] -> [[Int]] -> Bool
isIncluded current list = any includes list
where
includes :: [Int] -> Bool
includes range' = all (`elem` range') current
coveredByPartner :: [[Int]] -> Bool
coveredByPartner [first,second] = isIncluded first [second] || isIncluded second [first]
main = do
input <- readFile "four-input.txt"
let lists= (map (parseLine) $ lines input)
print $ length $ filter id $ map coveredByPartner lists