Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

solution to 79

  • Loading branch information...
commit 534b59052caac5826fda98734e87aaf8fbc3183e 1 parent e43a16b
@eklitzke authored
Showing with 87 additions and 0 deletions.
  1. +37 −0 79/EulerProblem.hs
  2. +50 −0 79/keylog.txt
View
37 79/EulerProblem.hs
@@ -0,0 +1,37 @@
+import System.IO.Unsafe
+import Control.Monad
+
+splitUntilFirst :: Eq a => a -> [a] -> ([a], [a])
+splitUntilFirst t xs =
+ let a = takeWhile (\x -> x /= t) xs
+ b = drop (length a) xs
+ in (a, b)
+
+attemptChar :: Char -> String -> String -> (Bool, String)
+attemptChar c notAllowed s =
+ let (a, b) = splitUntilFirst c s
+ in case b of
+ [] -> (False, [])
+ otherwise ->
+ let bl = (not $ any (\x -> x `elem` notAllowed) a)
+ in (bl, b)
+
+attempt :: String -> String -> Bool
+attempt pwd truth =
+ case pwd of
+ (x:xs) ->
+ let (b, rem) = attemptChar x xs truth
+ in b && (attempt xs rem)
+ --in (unsafePerformIO $ print x) `seq` (unsafePerformIO $ print (b, rem)) `seq`(b && attempt xs rem)
+ otherwise -> True
+
+possibleAnswer :: Int -> [String] -> Bool
+possibleAnswer n attempts =
+ let sn = show n
+ in all (\x -> attempt x sn) attempts
+
+main = do
+ xs <- liftM lines $ readFile "keylog.txt"
+ let ls = map init xs
+ pa x = possibleAnswer x ls
+ print $ head [x | x <- [100..], pa x]
View
50 79/keylog.txt
@@ -0,0 +1,50 @@
+319
+680
+180
+690
+129
+620
+762
+689
+762
+318
+368
+710
+720
+710
+629
+168
+160
+689
+716
+731
+736
+729
+316
+729
+729
+710
+769
+290
+719
+680
+318
+389
+162
+289
+162
+718
+729
+319
+790
+680
+890
+362
+319
+760
+316
+729
+380
+319
+728
+716
Please sign in to comment.
Something went wrong with that request. Please try again.