Jul 18, 2008 07:55
С детства хотел на практике проверить такой метод:
foldMessage n xs = zipWith xor xs (drop n xs)
task1 = map chr guess1 where
keyLen = 3
foldedMsg = foldMessage keyLen message
guess1 = nub $ guesses where
guess = map ord "the "
guessLen = length guess
folded1 = foldMessage keyLen guess
indices = locate folded1 foldedMsg
guesses = [ k | i <- indices,
let kd = zipWith xor guess (drop i message),
checkKey kd,
let k = rotations (take keyLen kd) !! (i `mod` keyLen)
]
checkKey xs = isCyclic && isLower where
isCyclic = and $ zipWith (==) xs (drop keyLen xs)
isLower = and $ map (\ x -> (ord 'a'<=x)&&(x <= ord 'z')) xs
locate xs ys = elemIndices xs lookupData where
lookupData = [take m (z:zs) | (z,zs) <- heads ys]
m = length xs
Для данного задания дает 5 догадок без какого-либо частотного анализа и прочей ерунды.
projecteuler.net,
haskell