From 155f4051896a156c5d6b20c10a9d5b90e0bbab89 Mon Sep 17 00:00:00 2001 From: qvalentin Date: Sun, 11 Dec 2022 14:25:56 +0100 Subject: [PATCH] finish day four --- four-easy.hs | 31 +++++++++++++++++++++++++++++++ four.hs | 28 ++++++++++++++++++++++++++++ four2.hs | 25 +++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 four-easy.hs create mode 100644 four.hs create mode 100644 four2.hs diff --git a/four-easy.hs b/four-easy.hs new file mode 100644 index 0000000..bbac13a --- /dev/null +++ b/four-easy.hs @@ -0,0 +1,31 @@ +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 diff --git a/four.hs b/four.hs new file mode 100644 index 0000000..a6401b1 --- /dev/null +++ b/four.hs @@ -0,0 +1,28 @@ +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 diff --git a/four2.hs b/four2.hs new file mode 100644 index 0000000..a9673ab --- /dev/null +++ b/four2.hs @@ -0,0 +1,25 @@ +import Control.Monad +import Data.Char +import Data.List + +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)] + + + + +coveredByPartner :: [[Int]] -> Bool +coveredByPartner [first,second] = 0 /= (length $ intersect first second) + + +main = do + input <- readFile "four-input.txt" + let lists= (map (parseLine) $ lines input) + print $ length $ filter id $ map coveredByPartner lists