Last active
December 15, 2022 15:54
-
-
Save kinnison/894af344ad6b56bd0de784ac1a72e088 to your computer and use it in GitHub Desktop.
Shonky attempt at queens problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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