From 2d02f553aa4cc4ded630628eccdf34f55937cee5 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Wed, 14 Dec 2016 20:19:02 +0100 Subject: Add 2015 sources --- 2015/day11.hs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 2015/day11.hs (limited to '2015/day11.hs') 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 -- cgit v1.2.3-54-g00ecf