Skip to content

Instantly share code, notes, and snippets.

@kinnison
Last active December 15, 2022 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kinnison/894af344ad6b56bd0de784ac1a72e088 to your computer and use it in GitHub Desktop.
Save kinnison/894af344ad6b56bd0de784ac1a72e088 to your computer and use it in GitHub Desktop.
Shonky attempt at queens problem
def queens(n, i, a, b, c):
if i < n:
for j in range(n):
if j not in a and i+j not in b and i-j not in c:
yield from queens(n, i+1, a+[j], b+[i+j], c+[i-j])
else:
yield a
def sudoku_ok(solution):
houses = [int((x-1)/3) for x in solution]
print(f" Houses: {houses}")
# Valid *iff* houses[0:3] are unique, houses[3:6], and houses[6:9]
ok = len(set(houses[0:3])) == 3
ok = ok and len(set(houses[3:6])) == 3
ok = ok and len(set(houses[6:9])) == 3
return ok
for solution in queens(9, 0, [], [], []):
if sudoku_ok(solution):
print(solution)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment