diff options
author | tomsmeding <tom.smeding@gmail.com> | 2016-12-14 20:19:02 +0100 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2016-12-14 20:19:02 +0100 |
commit | 2d02f553aa4cc4ded630628eccdf34f55937cee5 (patch) | |
tree | d5377ebdff68788725b5820d5331ce7b6c9d4a84 /2015/day11.hs | |
parent | 97b4c5d86cc12447ac6845e25a863e26a88aec35 (diff) |
Add 2015 sources
Diffstat (limited to '2015/day11.hs')
-rw-r--r-- | 2015/day11.hs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/2015/day11.hs b/2015/day11.hs new file mode 100644 index 0000000..f3c05b3 --- /dev/null +++ b/2015/day11.hs @@ -0,0 +1,52 @@ +module Main where + +import Data.Char +import Numeric + +pairs :: String -> [(Char,Char)] +pairs (a:b:cs) = (a,b) : pairs (b:cs) +pairs _ = [] + +allEqual :: (Eq a) => [a] -> Bool +allEqual (a:b:cs) = if a /= b then False else allEqual (b:cs) +allEqual _ = True + + +hasStraight :: String -> Bool +hasStraight (a:b:c:ds) = (y == x + 1 && z == x + 2) || hasStraight (b:c:ds) + where (x,y,z) = (ord a,ord b,ord c) +hasStraight _ = False + +hasForbidden :: String -> Bool +hasForbidden [] = False +hasForbidden (x:xs) = x /= 'i' && x /= 'o' && x /= 'l' && not (hasForbidden xs) + +hasTwoPairs :: String -> Bool +hasTwoPairs s = (not.allEqual) $ filter (\(a,b) -> a == b) (pairs s) + +isValid :: String -> Bool +isValid s = hasStraight s && not (hasForbidden s) && hasTwoPairs s + + +show26 :: Int -> String +show26 n = showIntAtBase 26 (\d -> chr (97 + d)) n "" + +read26 :: String -> Int +read26 = fst . head . readInt 26 (\c -> let o = ord c in 97 <= o && o <= 122) (\c -> ord c - 97) + +pad :: Int -> Char -> String -> String +pad n c s = if length s < n then pad n c (c:s) else s + + +nextpass :: String -> String +nextpass s = pad (length s) 'a' $ show26 $ succ $ read26 s + +nextvalidpass :: String -> String +nextvalidpass = head . dropWhile (not . isValid) . iterate nextpass + + +day11 :: IO () +day11 = do + print $ nextvalidpass $ nextpass $ nextvalidpass "cqjxjnds" + +main = day11 |