From 010cf614a510a83e89d4a610dee994f6e25be72e Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Thu, 10 Dec 2020 18:06:35 +0100 Subject: [PATCH] Add 2015 day 11 --- haskellAoC/inputs/2015/11 | 1 + haskellAoC/src/Y2015/Day11.hs | 35 +++++++++++++++++++++++++++++++++++ haskellAoC/src/Y2015/Days.hs | 2 ++ 3 files changed, 38 insertions(+) create mode 100644 haskellAoC/inputs/2015/11 create mode 100644 haskellAoC/src/Y2015/Day11.hs diff --git a/haskellAoC/inputs/2015/11 b/haskellAoC/inputs/2015/11 new file mode 100644 index 0000000..cdc0b85 --- /dev/null +++ b/haskellAoC/inputs/2015/11 @@ -0,0 +1 @@ +cqjxjnds \ No newline at end of file diff --git a/haskellAoC/src/Y2015/Day11.hs b/haskellAoC/src/Y2015/Day11.hs new file mode 100644 index 0000000..732e0ef --- /dev/null +++ b/haskellAoC/src/Y2015/Day11.hs @@ -0,0 +1,35 @@ +module Y2015.Day11 (y15day11) where + +import Data.Char +import Data.List + +allTrue :: [(a -> Bool)] -> a -> Bool +allTrue predicates entry = all (== True) $ map ($ entry) predicates + +rowOfThree :: String -> Bool +rowOfThree (x1:x2:x3:xs) + | c3 == (c2 + 1) && c2 == (c1 + 1) = True + | otherwise = rowOfThree (x2:x3:xs) + where c1 = ord x1 + c2 = ord x2 + c3 = ord x3 +rowOfThree _ = False + +-- Do we accept triples as 1 pair ? +twoPairs :: String -> Bool +twoPairs s = (>= 2) . length $ filter (== 2) $ map length $ group s + +isValid :: String -> Bool +isValid s = allTrue [validChars, length8, rowOfThree, twoPairs] s + where validChars = all (\c -> isLower c && (c `notElem` "iol")) + length8 = (== 8) . length + +nextSequence :: String -> String +nextSequence s = reverse $ nextSequence' $ reverse $ s + where nextSequence' ('z':cs) = 'a':nextSequence' cs + nextSequence' (c:cs) = ((chr . (+1) . ord) $ c) : cs + nextSequence' [] = [] + +y15day11 :: [String] -> (String, String) +y15day11 (input:_) = (part1, part2) + where [part1, part2] = take 2 $ filter isValid $ iterate nextSequence input diff --git a/haskellAoC/src/Y2015/Days.hs b/haskellAoC/src/Y2015/Days.hs index d5783af..2a44898 100644 --- a/haskellAoC/src/Y2015/Days.hs +++ b/haskellAoC/src/Y2015/Days.hs @@ -10,6 +10,7 @@ import Y2015.Day07 import Y2015.Day08 import Y2015.Day09 import Y2015.Day10 +import Y2015.Day11 year2015 :: String -> [String] -> (String, String) year2015 "01" = y15day01 @@ -22,3 +23,4 @@ year2015 "07" = y15day07 year2015 "08" = y15day08 year2015 "09" = y15day09 year2015 "10" = y15day10 +year2015 "11" = y15day11