Skip to content

Instantly share code, notes, and snippets.

@woodrush
Created July 4, 2022 01:26
Show Gist options
  • Save woodrush/0ca4d84a3aa729ed2a350598a63f65db to your computer and use it in GitHub Desktop.
Save woodrush/0ca4d84a3aa729ed2a350598a63f65db to your computer and use it in GitHub Desktop.
Card Puzzle
# A solver for https://twitter.com/ikeikey/status/1543710547874615297
import numpy as np
def search(A, l_count, handlist, i, j):
if i == 7 and j == 7:
return A
elif i == j:
return search(A, l_count, handlist, i, j+1)
l_count = list(l_count)
handlist = list(handlist)
handlist.append(None)
A = np.array(A)
for x in range(8):
if x == i or x == j:
continue
if l_count[x] >= 7:
continue
if A[j,i] == x:
continue
hands = tuple(sorted([i,j,x]))
if hands in handlist:
continue
handlist[-1] = hands
l_count[x] += 1
A[i,j] = x
if j == 7:
status = search(A, l_count, handlist, i+1, 0)
else:
status = search(A, l_count, handlist, i, j+1)
if status is not None:
return status
l_count[x] -= 1
return None
A = np.ones((8,8), dtype=np.int64) * -1
l_count = [0]*8
handlist = []
A = search(A, l_count, handlist, 0, 0)
print(A+1)
d_strategy = {}
for i in range(8):
for j in range(8):
answer = A[i,j]
hands = tuple(sorted([i,j,answer]))
assert hands not in d_strategy.keys()
if answer != -1:
d_strategy[hands] = (i,j), answer
print(len(d_strategy.keys()))
for k,v in sorted(d_strategy.items(), key=lambda x:x[0]):
k = tuple(np.array(k, dtype=np.int64)+1)
v0 = tuple(np.array(v[0], dtype=np.int64)+1)
v1 = v[1] + 1
print(k, ":", v0, "->", v1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment