Created
October 18, 2023 14:25
-
-
Save NoodleSushi/ac1957002511cf5b38c3a47489b1955d to your computer and use it in GitHub Desktop.
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
from copy import deepcopy | |
SIZE = 5 | |
ITEMS = SIZE * SIZE | |
table = [ | |
[-1, -1, -1, -1, -1], | |
[-1, -1, -1, -1, -1], | |
[-1, -1, -1, -1, -1], | |
[-1, -1, -1, -1, -1], | |
[-1, -1, -1, -1, -1], | |
] | |
def print_table(table): | |
for y in table: | |
print(', '.join([ str(i+1) if i != -1 else '_' for i in y ])) | |
class TableSolver: | |
def __init__(self) -> None: | |
self.orig_table = [ [-1]*SIZE for _ in range(SIZE)] | |
self.table = [ [-1]*SIZE for _ in range(SIZE)] | |
self.idx = 0 | |
self.last_backed = False | |
@property | |
def orig_item(self): | |
return self.orig_table[self.idx // SIZE][self.idx % SIZE] | |
@property | |
def item(self): | |
return self.table[self.idx // SIZE][self.idx % SIZE] | |
@item.setter | |
def item(self, x): | |
self.table[self.idx // SIZE][self.idx % SIZE] = x | |
@property | |
def is_given(self): | |
return self.orig_table[self.idx // SIZE][self.idx % SIZE] != -1 | |
def travel(self): | |
self.idx = self.idx + 1 | |
self.last_backed = False | |
def back(self): | |
self.idx = self.idx - 1 | |
self.last_backed = True | |
def find_next_num(self): | |
choices = set(range(SIZE)) | |
choices.difference_update(self.table[self.idx // SIZE]) | |
choices.difference_update([self.table[y][self.idx % SIZE] for y in range(SIZE)]) | |
choices = [x for x in choices if x > self.item] | |
if len(choices) == 0: | |
return None | |
return min(choices) | |
def solve(self, table): | |
self.orig_table = deepcopy(table) | |
self.table = deepcopy(self.orig_table) | |
while self.idx < ITEMS: | |
if self.is_given: | |
if self.last_backed: | |
self.back() | |
else: | |
self.travel() | |
else: | |
next_num = self.find_next_num() | |
if next_num == None: | |
self.item = -1 | |
self.back() | |
else: | |
self.item = next_num | |
self.travel() | |
return deepcopy(self.table) | |
table = TableSolver().solve(table) | |
print_table(table) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment