forked from glnmario/hyperactive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHyper-encoding
More file actions
63 lines (58 loc) · 2.89 KB
/
Hyper-encoding
File metadata and controls
63 lines (58 loc) · 2.89 KB
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
61
62
63
#function which calculates the top left of the hypersquare a given index is in
def top_left_hypersquare(variable, length_of_sudoku):
top_left = [2,2]
row, column = variable.copy()[0], variable.copy()[1]
while row > sqrt(length_of_sudoku) + 1:
row -= sqrt(length_of_sudoku) + 1
top_left[0] += int(sqrt(length_of_sudoku) + 1)
while column > sqrt(length_of_sudoku) + 1:
column -= sqrt(length_of_sudoku) + 1
top_left[1] += int(sqrt(length_of_sudoku) + 1)
if row == 1:
return None
if column == 1:
return None
return top_left
#function which, given two variables, decides if they are in the same hypersquare with
#the same value
def incompatible_hyperBlock(var_1, var_2, length_of_sudoku):
if var_1 == var_2:
return False
if top_left_hypersquare(var_1, length_of_sudoku) == None:
return False
if top_left_hypersquare(var_2, length_of_sudoku) == None:
return False
if var_1[2] == var_2[2]:
if top_left_hypersquare(var_1, length_of_sudoku) == top_left_hypersquare(var_2, length_of_sudoku):
return True
return False
#function which outputs a list of hypersquare top left coordinates
def generate_top_left_of_hypersquares(length_of_sudoku):
top_lefts_list = []
for row in range(2, length_of_sudoku + 1, int(sqrt(length_of_sudoku) + 1)):
for column in range(2, length_of_sudoku + 1, int(sqrt(length_of_sudoku) + 1)):
top_lefts_list.append([row, column])
return top_lefts_list
#function which says every number must appear at least once in each hypersquare
def at_least_hypersquare(length_of_sudoku):
encoding = []
for top_lefts in generate_top_left_of_hypersquares(length_of_sudoku):
for row in range(int(sqrt(length_of_sudoku))):
for column in range(int(sqrt(length_of_sudoku))):
encoding.append([])
for value in range(1, length_of_sudoku + 1):
encoding[-1].append(encode_into_number([top_lefts[0] + row, top_lefts[1] + column, value], s_test))
return encoding
#print(at_least_hypersquare(81))
#function which says every number can appear at most once in each hypersquare(NEEDS OPTIMISING)
def at_most_hypersquare(length_of_sudoku):
encoding = []
for row in range(1, length_of_sudoku + 1):
for column in range(1, length_of_sudoku + 1):
for value in range(1, length_of_sudoku + 1):
for row2 in range(1, length_of_sudoku + 1):
for column2 in range(1, length_of_sudoku + 1):
for value2 in range(1, length_of_sudoku + 1):
if incompatible_hyperBlock([row, column, value], [row2, column2, value2], length_of_sudoku):
encoding.append([-encode_into_number([row, column, value], s_test), -encode_into_number([row2, column2, value2], s_test)])
return encoding