mirror of
https://github.com/mx42/adventofcode.git
synced 2026-01-14 22:09:50 +01:00
Add 2015 day 11
This commit is contained in:
35
haskellAoC/src/Y2015/Day11.hs
Normal file
35
haskellAoC/src/Y2015/Day11.hs
Normal 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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user