Add 2015 day 11

This commit is contained in:
Xavier Morel
2020-12-10 18:06:35 +01:00
parent a7e4737b00
commit 010cf614a5
3 changed files with 38 additions and 0 deletions

View File

@@ -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

View File

@@ -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