Advertisement
danchaofan

Euler #59

Nov 17th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. encoded = list(open('p059_cipher.txt'))[0]
  2. reformat, stringName = [], ""
  3. for a in encoded:
  4.     if a == ",":
  5.         reformat.append(int(stringName))
  6.         stringName = ""
  7.         continue
  8.     stringName += a
  9. possibleEntries, possibleKeys = [], []
  10. for b in range(97, 123):
  11.     for c in range(97, 123):
  12.         for d in range(97, 123):
  13.             key, index, decrypted = [b, c, d], 0, []
  14.             for e in reformat:
  15.                 selectDecryption = (key[index] ^ e)
  16.                 if selectDecryption <= 31:
  17.                     break
  18.                 if selectDecryption == 127:
  19.                     break
  20.                 decrypted.append(selectDecryption)
  21.                 if index == 2:
  22.                     index = 0
  23.                     continue
  24.                 index += 1
  25.             if len(decrypted) != len(reformat):
  26.                 continue
  27.             possibleEntries.append(decrypted)
  28.             possibleKeys.append(key)
  29. filtered = []
  30. for f in possibleEntries:
  31.     if f[0] == 40:
  32.         filtered.append(f)
  33.     if f[0] == 42:
  34.         filtered.append(f)
  35. refiltered = []
  36. for g in filtered:
  37.     readable = [chr(h) for h in g]
  38.     refiltered.append(readable)
  39. for i in refiltered:
  40.     if i[1] == "T":
  41.         if i[2] == "h":
  42.             if i[3] == "e":
  43.                 answer = i
  44. answer = [ord(j) for j in answer]
  45. total = 0
  46. for k in answer:
  47.     total += k
  48. total += 46
  49. print(total)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement