1

Running in SuSE linux,

This is a quick and dirty script to solve a geocaching puzzle (GC9K63A).
First attempt to do anything in python. I added the print "." to give proof of life, but dot doesn't appear. Previously left running for a week with out a result.

   items = [ 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]
    from itertools import permutations
    for perm in permutations(items):
    
     A=perm[0]
     B=perm[1]
     C=perm[2]
     D=perm[3]
     E=perm[4]
     F=perm[5]
     G=perm[6]
     H=perm[7]
     I=perm[8]
     J=perm[9]
     K=perm[10]
     L=perm[11]
     M=perm[12]
     N=perm[13]
     O=perm[14]
     P=perm[15]
     Q=perm[16]
     R=perm[17]
     S=perm[18]
     T=perm[19]
     U=perm[20]
     V=perm[21]
     W=perm[22]
     X=perm[23]
     Y=perm[24]
     Z=perm[25]
    
    
     if     (A - B - C - D - E - F + G + H + I - J - K + L - M + N - O + P + Q + R + S - T - U - V - W - X - Y + Z) == -55 and (A - B - C - D + E - F + G - H + I - J - K - L - M + N - O + P - Q + R - S - T - U + V + W + X - Y + Z) == -37 and (A + B + C - D + E - F + G - H + I + J - K + L + M - N - O + P - Q - R + S - T + U - V - W + X - Y - Z) == -27 and (A + B - C - D - E + F + G - H + I + J + K + L + M + N - O - P + Q + R - S - T - U - V + W - X - Y + Z) == 13 and (A + B - C + D - E + F + G + H - I + J + K + L - M + N + O - P + Q - R - S - T - U - V - W - X - Y + Z) == -59 and (A - B - C + D - E - F + G - H + I - J - K + L - M + N + O + P - Q + R - S - T - U + V + W - X - Y + Z) == 7 and (A - B - C - D - E + F + G - H + I - J - K + L + M - N + O - P + Q + R - S + T + U + V - W - X + Y + Z) == 91 and (A - B + C + D - E + F + G + H + I - J + K - L + M - N - O - P - Q + R + S - T - U + V + W + X - Y - Z) == 19 and (A + B + C - D + E + F - G - H - I + J - K + L - M + N - O + P - Q + R + S - T - U + V + W - X - Y - Z) == -51 and (A - B - C + D - E - F - G + H + I + J + K + L + M + N - O + P - Q + R + S + T - U + V + W + X - Y - Z) == 79 and (A - B + C + D - E - F - G - H - I - J - K - L + M - N + O + P - Q + R - S - T + U + V + W + X + Y - Z) == 29 and (A + B - C - D - E - F + G + H + I - J + K - L + M + N + O + P + Q - R - S - T + U - V - W - X - Y + Z) == -5 and (A + B - C + D + E - F + G + H - I - J + K + L + M + N - O - P - Q + R - S + T - U + V - W + X - Y - Z) == -5 and (A + B - C - D - E + F - G + H - I - J - K - L - M + N - O + P + Q + R + S + T + U - V - W + X - Y + Z) == 55 and (A - B + C + D + E + F + G + H - I - J + K - L + M - N - O + P + Q - R + S - T - U + V + W + X - Y + Z) == 1 and (A - B + C - D - E - F - G - H - I + J + K - L + M - N - O - P + Q + R + S - T + U + V - W + X - Y - Z) == -57 and (A + B - C - D - E - F - G + H - I - J - K - L - M + N - O - P + Q + R - S - T - U - V - W + X - Y + Z) == -117 and (A + B - C + D + E - F - G - H - I + J + K - L + M + N - O - P + Q + R - S - T + U + V - W + X + Y + Z) == 51 and (A - B + C - D - E - F - G + H - I + J - K + L - M - N + O + P + Q - R - S + T + U - V - W + X + Y + Z) == 3 
    
    
    
         print  A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z
    
    else:
         print "."


Kenziem
  • 13
  • 4
  • Does this even run? You're missing the `:` at the end of the `if` line. – Barmar Apr 01 '22 at 20:53
  • I tried it and it immediately started printing thousands of `.` lines. – Barmar Apr 01 '22 at 20:55
  • 2
    This is Python 2 syntax -- you should upgrade to Python 3 if possible. – Barmar Apr 01 '22 at 20:56
  • 1
    The number of permutation of 26 elements is about 4*(10^26). If you check 10^9 permutations per second, the code will take over 12 billion years to finish. – bb1 Apr 02 '22 at 00:17
  • BTW, I'd just like to politely remind you that publishing spoilers for geocaches without the cache owner's consent is forbidden by the geocaching.com [terms of use](https://www.geocaching.com/policies/en/terms-of-use). It's also rude and can spoil other people's fun. In this particular case, however, *I don't think your question actually counts as a spoiler*, since your program — even if fixed as described below — can't be used to solve the puzzle in any reasonable time and reveals no clues that aren't already given on the [cache page](https://coord.info/GC9K63A) itself. – Ilmari Karonen Apr 02 '22 at 10:21
  • Also BTW, this puzzle can certainly be solved with Python. Just not like this. And that's all I will say. ;) – Ilmari Karonen Apr 02 '22 at 11:19

1 Answers1

1

There are a few details to point out here.

First off, you said you let this code run for a week without a result.
Notice that, even if a result is found, it would not stop. So if you haven't looked through the entire output, there is a good chance there actually was a result, but it got washed away by the dots following after it.
Adding such a condition would be as easy as including a break statement after you printed your result.

Next, in the statements right after your for loop begins, you step for step assign new values to the different variables named by letters. This can be done via unpacking within the forloop. This should lead to a slight speedup, but also just makes the code easier to read.

Lastly, the io operation of printing may actually slow your code down significantly.
To be sure of this, I tried this out:
I added a counter at the beginning, and added to the print statement this counter.
That way I could see how many permutations the script could handle within a given time frame.
Running it about 10 seconds leads to about 100000 permutations being checked.
Then I added an additional if clause, that checks if the counter is divisible by 100000, and only then prints the line.
In the same 10 seconds, this version of the script checks 2500000 permutations.

The modified code I arrived at at the very end looks like this:

counter = 0
items = [ 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]
from itertools import permutations
for A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z in permutations(items):
  if (A - B - C - D - E - F + G + H + I - J - K + L - M + N - O + P + Q + R + S - T - U - V - W - X - Y + Z) == -55 and (A - B - C - D + E - F + G - H + I - J - K - L - M + N - O + P - Q + R - S - T - U + V + W + X - Y + Z) == -37 and (A + B + C - D + E - F + G - H + I + J - K + L + M - N - O + P - Q - R + S - T + U - V - W + X - Y - Z) == -27 and (A + B - C - D - E + F + G - H + I + J + K + L + M + N - O - P + Q + R - S - T - U - V + W - X - Y + Z) == 13 and (A + B - C + D - E + F + G + H - I + J + K + L - M + N + O - P + Q - R - S - T - U - V - W - X - Y + Z) == -59 and (A - B - C + D - E - F + G - H + I - J - K + L - M + N + O + P - Q + R - S - T - U + V + W - X - Y + Z) == 7 and (A - B - C - D - E + F + G - H + I - J - K + L + M - N + O - P + Q + R - S + T + U + V - W - X + Y + Z) == 91 and (A - B + C + D - E + F + G + H + I - J + K - L + M - N - O - P - Q + R + S - T - U + V + W + X - Y - Z) == 19 and (A + B + C - D + E + F - G - H - I + J - K + L - M + N - O + P - Q + R + S - T - U + V + W - X - Y - Z) == -51 and (A - B - C + D - E - F - G + H + I + J + K + L + M + N - O + P - Q + R + S + T - U + V + W + X - Y - Z) == 79 and (A - B + C + D - E - F - G - H - I - J - K - L + M - N + O + P - Q + R - S - T + U + V + W + X + Y - Z) == 29 and (A + B - C - D - E - F + G + H + I - J + K - L + M + N + O + P + Q - R - S - T + U - V - W - X - Y + Z) == -5 and (A + B - C + D + E - F + G + H - I - J + K + L + M + N - O - P - Q + R - S + T - U + V - W + X - Y - Z) == -5 and (A + B - C - D - E + F - G + H - I - J - K - L - M + N - O + P + Q + R + S + T + U - V - W + X - Y + Z) == 55 and (A - B + C + D + E + F + G + H - I - J + K - L + M - N - O + P + Q - R + S - T - U + V + W + X - Y + Z) == 1 and (A - B + C - D - E - F - G - H - I + J + K - L + M - N - O - P + Q + R + S - T + U + V - W + X - Y - Z) == -57 and (A + B - C - D - E - F - G + H - I - J - K - L - M + N - O - P + Q + R - S - T - U - V - W + X - Y + Z) == -117 and (A + B - C + D + E - F - G - H - I + J + K - L + M + N - O - P + Q + R - S - T + U + V - W + X + Y + Z) == 51 and (A - B + C - D - E - F - G + H - I + J - K + L - M - N + O + P + Q - R - S + T + U - V - W + X + Y + Z) == 3 :
    print (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z)
    break
  else:
    counter += 1
    if counter % 100000 == 0:
      print (".", counter)

It's also worthwile to keep in mind that the amount of permutations to check here is just pretty gigantic, so even with these optimizations, it will likely still take quite a while.
To be precise, the amount of permutations to check here is !25 = 15511210043330985984000000, which even if you would be able to increase the check rate by a million fold, would still take about two million years.
Unless you have a few livetimes to wait, you are probably better off looking for a analytical solution

Jonas V
  • 668
  • 2
  • 5
  • 20
  • Edited the code as suggested. It is still running but at least I know it is not stuck in a loop. Most puzzles only use the digits 1-9, so not giving up hope entirely. it is less work than implementing RPG logic in a spreadsheet. ('.', 134414500000) – Kenziem Apr 03 '22 at 16:21
  • Found numpy, I have trouble with matirx algebra because I learned APL first and they handle matrix operations differently. – Kenziem Apr 06 '22 at 16:56