summaryrefslogtreecommitdiff
path: root/euler59.py
diff options
context:
space:
mode:
authorIru Cai <mytbk920423@gmail.com>2018-05-27 13:25:10 +0800
committerIru Cai <mytbk920423@gmail.com>2018-05-27 13:25:10 +0800
commit12b78407f3ce4d6ee5f67fb0c6ce5c754cf78a58 (patch)
treec5add6603cb45857977a797f251d9d123464fb57 /euler59.py
parent1c76beb0d4ccf57d2db84cf417f37a3a155fb905 (diff)
downloadproject_euler-12b78407f3ce4d6ee5f67fb0c6ce5c754cf78a58.tar.xz
57, 59, 67, 206
Diffstat (limited to 'euler59.py')
-rw-r--r--euler59.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/euler59.py b/euler59.py
new file mode 100644
index 0000000..fe297c0
--- /dev/null
+++ b/euler59.py
@@ -0,0 +1,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))