Project Euler 解答

Project Euler Problem 059

Project Euler Problem 059

```import Control.Arrow
import Data.Ord
import Data.Char
import Data.List
import Data.Bits(xor)
import System.IO.Unsafe(unsafePerformIO)
filedata :: String
filedata = unsafePerformIO \$ readFile "059cipher1.txt"
values :: [Int]
values = read ("[" ++ filedata ++ "]") :: [Int]
alh :: [Int]
alh = map ((+ (ord 'a'))) [0..25]
--32以上122以下がアルファベット
decr :: [Int] -> [[Int]]
decr l = [sym | x<-alh,y<-alh,z<-alh,let sym=x:y:[z],let pas=cycle sym,check (zipWith xor pas l)] where
check lst = all (((>=32)&&&(<=122))>>>uncurry(&&)) lst
-- 多分聖書
decgod :: [Int] -> [Char]
decgod l = map chr \$ zipWith xor l \$ cycle [103,111,100]
dec :: [Int] -> [Int]
dec l = zipWith xor l \$ cycle [103,111,100]

ans1d :: [Char]
ans1d = decgod values

ans1 :: Int
ans1 = sum \$ dec values
--107359
main :: IO ()
main = print ans1

ans2 :: IO ()
ans2 = do
let
keys = [ [a,b,c] | a <- [97..122], b <- [97..122], c <- [97..122] ]
allAlpha = all (\k -> let a = ord k in (a >= 32 && a <= 122))
howManySpaces = length . filter (==' ')
cipher = (read ("[" ++ s ++ "]") :: [Int])
decrypts = [ map chr (zipWith xor (cycle key) cipher) | key <- keys ]