summaryrefslogtreecommitdiff
path: root/euler59.py
blob: fe297c0f066ea8a9f62d1a587a20ddb7e3faf387 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python

cipher_bytes = list(map(int, (input().split(','))))
cipher_with_index = list(zip(range(0, len(cipher_bytes)), cipher_bytes))

def decrypt(key, cwi):
    def dec(tup):
        i, c = tup
        return c^key[i%3]

    return list(map(dec, cwi))

def toText(t):
    s = ''
    for c in t:
        s = s + chr(c)
    return s

def asciisum(t):
    s = 0
    for c in t:
        s = s + c
    return s


def allprint(a):
    for c in a:
        if c >= 0x7f or c < 0x20:
            return False
    return True


key = [ord('a')] * 3

for key[0] in range(ord('a'), ord('z')+1):
    for key[1] in range(ord('a'), ord('z')+1):
        for key[2] in range(ord('a'), ord('z')+1):
            decrypted = decrypt(key, cipher_with_index)
            if allprint(decrypted):
                print(toText(decrypted))
                print(asciisum(decrypted))