-
Notifications
You must be signed in to change notification settings - Fork 1
/
mallory_fake_solution.py
60 lines (50 loc) · 1.99 KB
/
mallory_fake_solution.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from sys import exit
import numpy as np
print_progress = True
ai_escargot = np.array([
[1, 0, 0, 0, 0, 7, 0, 9, 0],
[0, 3, 0, 0, 2, 0, 0, 0, 8],
[0, 0, 9, 6, 0, 0, 5, 0, 0],
[0, 0, 5, 3, 0, 0, 9, 0, 0],
[0, 1, 0, 0, 8, 0, 0, 0, 2],
[6, 0, 0, 0, 0, 4, 0, 0, 0],
[3, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 4, 0, 0, 0, 0, 0, 0, 7],
[0, 0, 7, 0, 0, 0, 3, 0, 0],
])
def correct_cols(grid):
return all([all([sum(col == i) <= 1 for i in range(1, 10)]) for col in grid.T])
def correct_rows(grid):
return all([all([sum(row == i) <= 1 for i in range(1, 10)]) for row in grid])
def correct_boxs(grid):
boxs_correct = True
for i in range(1, 10):
boxs_correct &= sum((grid[0:3,0:3] == i).flatten()) <= 1
boxs_correct &= sum((grid[0:3,3:6] == i).flatten()) <= 1
boxs_correct &= sum((grid[0:3,6:9] == i).flatten()) <= 1
boxs_correct &= sum((grid[3:6,0:3] == i).flatten()) <= 1
boxs_correct &= sum((grid[3:6,3:6] == i).flatten()) <= 1
boxs_correct &= sum((grid[3:6,6:9] == i).flatten()) <= 1
boxs_correct &= sum((grid[6:9,0:3] == i).flatten()) <= 1
boxs_correct &= sum((grid[6:9,3:6] == i).flatten()) <= 1
boxs_correct &= sum((grid[6:9,6:9] == i).flatten()) <= 1
return boxs_correct
def selected_properties(grid):
return correct_cols(grid) and correct_rows(grid)
# return correct_cols(grid) and correct_boxs(grid)
# return correct_rows(grid) and correct_boxs(grid)
# return correct_cols(grid) and correct_rows(grid) and correct_boxs(grid)
def find_solution(grid):
if sum((grid == 0).flatten()) <= 0:
if selected_properties(grid):
print(grid)
exit(0)
return
next_empty = list(zip(*np.where(grid == 0)))[0]
for i in range(1, 10):
grid[next_empty] = i
if selected_properties(grid):
if print_progress: print(grid, end="\n\n\n")
find_solution(grid)
grid[next_empty] = 0
find_solution(ai_escargot)