summaryrefslogtreecommitdiff
path: root/euler59.py
diff options
context:
space:
mode:
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))