euler/old-haskell/059.hs
2019-06-11 13:43:20 +02:00

20 lines
819 B
Haskell

-- Source forum project haskell
import Data.Bits
import Data.Char
import Data.List
keys = [ [a,b,c] | a <- [97..122], b <- [97..122], c <- [97..122] ]
allAlpha a = all (\k -> let a = ord k in ( a >= 32 && a <= 122)) a
howManySpaces x = length (elemIndices ' ' x)
compareBy f x y = compare (f x) (f y)
main = do
s <- readFile "cipher1.txt"
let
cipher = (read ("[" ++ s ++ "]") :: [Int])
decrypts = [ (map chr (zipWith xor (cycle key) cipher), map chr key) | key <- keys ]
alphaDecrypts = filter (\(x,y) -> allAlpha x) decrypts
message = maximumBy (\(x,y) (x',y') -> compareBy howManySpaces x x') alphaDecrypts
asciisum = sum (map ord (fst message))
putStrLn ("The message " ++ (fst message) ++ " was encoded with key " ++ (snd message))
putStrLn (show asciisum)