import Prelude
import Data.List
import Data.Char
toBin =reverse. unfoldr f
where f  = Nothing
f n =let (q,r) = divMod n 2 in Just(r,q)
toDigit = foldl1 f
where f a b = 2*a+b
xor ::Int->Int->Int
xor x y | len == = toDigit.zipWith xor' (toBin x) $toBin y
| len >  = toDigit.zipWith xor' (toBin x) $replicate len  ++ toBin y
| len <  = toDigit.zipWith xor' (toBin y) $replicate (-len) ++ toBin x
where len = (length.toBin)x - (length.toBin)y
xor' a b = if a == b then  else 1
decode xs key = map chr.zipWith xor xs .cycle .map ord $key
findKey xs = find (g.decode xs) [[a,b,c]|a<-keys,b<-keys,c<-keys]
where keys = "abcdefghijklmopqrstuvwxyz"
g ys =all (flip isInfixOf ys)["and ","is "]
main = do f <-readFile "cipher1.txt"
let plain = read $"["++f++"]" ::[Int]
--          print.findKey$plain ord.flip decode "god"$ plain



(The Gospel of John, chapter 1) 1 In the beginning the Word already existed. He was with God, and he was God. 2 He was in the beginning with God. 3 He created everything there is. Nothing exists that he didn’t make. 4 Life itself was in him, and this life gives light to everyone. 5 The light shines through the darkness, and the darkness can never extinguish it. 6 God sent John the Baptist 7 to tell everyone about the light so that everyone might believe because of his testimony. 8 John himself was not the light; he was only a witness to the light. 9 The one who is the true light, who gives light to everyone, was going to come into the world. 10 But although the world was made through him, the world didn’t recognize him when he came. 11 Even in his own land and among his own people, he was not accepted. 12 But to all who believed him and accepted him, he gave the right to become children of God. 13 They are reborn! This is not a physical birth resulting from human passion or plan, this rebirth comes from God.14 So the Word became human and lived here on earth among us. He was full of unfailing love and faithfulness. And we have seen his glory, the glory of the only Son of the Father.