advent-of-code/four-easy.hs

32 lines
1.1 KiB
Haskell

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)]
isIncludedElseWhere :: [[Int]] -> [Int] -> [[Int]] -> Int -> Int
isIncludedElseWhere previous current [] count=count
isIncludedElseWhere previous current next count=do
let found = (isIncluded current previous && isIncluded current next)
(if found then 1 else 0) +
(isIncludedElseWhere (current:previous) (head next) (tail next) count)
isIncluded :: [Int] -> [[Int]] -> Bool
isIncluded current list = any includes list
where
includes :: [Int] -> Bool
includes range' = all (`elem` range') current
main = do
input <- readFile "four-input2.txt"
let lists= ( join $ map (parseLine) $ lines input)
print $ isIncludedElseWhere [] (head lists) (tail lists) 0