Rough prototype in python
This commit is contained in:
parent
8431f78b50
commit
9ef5cad178
|
@ -0,0 +1,10 @@
|
|||
9.ox
|
||||
...........................
|
||||
....o......................
|
||||
............o..............
|
||||
...........................
|
||||
....o......................
|
||||
...............o...........
|
||||
...........................
|
||||
......o..............o.....
|
||||
..o.......o................
|
203
assets/solve.py
203
assets/solve.py
|
@ -2,51 +2,164 @@
|
|||
|
||||
import sys
|
||||
|
||||
def main():
|
||||
with open(sys.argv[1]) as b:
|
||||
board = b.read().splitlines()
|
||||
try:
|
||||
first_line = board[0][::-1]
|
||||
except:
|
||||
print("map error")
|
||||
sys.exit(1)
|
||||
board = board[1:]
|
||||
full = first_line[0]
|
||||
obstc = first_line[1]
|
||||
empty = first_line[2]
|
||||
try:
|
||||
num = int(first_line[3:][::-1])
|
||||
except:
|
||||
print("map error")
|
||||
sys.exit(2)
|
||||
if len(board) != num:
|
||||
print("map error")
|
||||
sys.exit(3)
|
||||
first_line_len = len(board[0])
|
||||
if any(first_line_len != len(line) for line in board):
|
||||
print("map error")
|
||||
sys.exit(4)
|
||||
converted_board = []
|
||||
for i, line in enumerate(board):
|
||||
converted_board.append(list(line))
|
||||
for i in range(len(board)):
|
||||
for j in range(first_line_len):
|
||||
val = board[i][j]
|
||||
if i < 1:
|
||||
top = 0
|
||||
else:
|
||||
top = converted_board[i - 1][j]
|
||||
if j < 1:
|
||||
left = 0
|
||||
else:
|
||||
left = converted_board[i][j - 1]
|
||||
if j < 1 or i < 1:
|
||||
top_left = 0
|
||||
else:
|
||||
top_left = converted_board[i - 1][j - 1]
|
||||
converted_board[i][j] = str(int(val == obstc)) + left + top + top_left
|
||||
print(end=converted_board[i][j])
|
||||
print()
|
||||
def to_i(idx, x):
|
||||
return idx // x
|
||||
|
||||
def to_j(idx, x):
|
||||
return idx % x
|
||||
|
||||
def plus_j(idx, n, max):
|
||||
if idx + n >= max:
|
||||
return -1
|
||||
return idx + n
|
||||
|
||||
def plus_i(idx, x, n, max):
|
||||
if idx + n * x >= max:
|
||||
return -1
|
||||
return idx + n * x
|
||||
|
||||
def is_obst(board, idx, obst):
|
||||
if board[idx] == obst:
|
||||
return 1
|
||||
else:
|
||||
return 0
|
||||
|
||||
def to_char(n):
|
||||
alph = '0123456789abcdefghijklmnopqrstuvwxyz'
|
||||
if n < len(alph):
|
||||
return alph[n]
|
||||
return "<OOB>"
|
||||
|
||||
def count_obst(prep_board, size, idx, x):
|
||||
max_idx = len(prep_board)
|
||||
if size == 0:
|
||||
return 0
|
||||
if plus_j(idx, size - 1, max_idx) == -1 or plus_i(idx, x, size - 1, max_idx) == -1:
|
||||
return -1
|
||||
idx = plus_j(plus_i(idx, x, -1, max_idx), -1, max_idx)
|
||||
top_left = 0
|
||||
top_right = 0
|
||||
bottom_left = 0
|
||||
bottom_right = 0
|
||||
if to_i(idx, x) != 0 and to_j(idx, x) != 0:
|
||||
top_left = prep_board[idx]
|
||||
if size == 1:
|
||||
return top_left
|
||||
if to_i(idx, x) != 0:
|
||||
top_right = prep_board[plus_j(idx, size, max_idx)]
|
||||
if to_j(idx, x) != 0:
|
||||
bottom_left = prep_board[plus_i(idx, x, size, max_idx)]
|
||||
bottom_right = prep_board[plus_j(plus_i(idx, x, size, max_idx), size, max_idx)]
|
||||
total = top_left + bottom_right - top_right - bottom_left
|
||||
print(f"{size=}")
|
||||
print(f"{top_right=}")
|
||||
print(f"{top_left=}")
|
||||
print(f"{bottom_right=}")
|
||||
print(f"{bottom_left=}")
|
||||
print(f"{total=}")
|
||||
return total
|
||||
|
||||
def print_prep_board_idx(prep_board, i, x, full):
|
||||
for idx in range(len(prep_board)):
|
||||
if i == idx:
|
||||
print(end=full)
|
||||
else:
|
||||
print(to_char(prep_board[idx]), end='')
|
||||
if to_j(idx, x) == x - 1:
|
||||
print()
|
||||
|
||||
def print_board_idx(board, i, x, full):
|
||||
for idx in range(len(board)):
|
||||
if i == idx:
|
||||
print(end=full)
|
||||
else:
|
||||
print(board[idx], end='')
|
||||
if to_j(idx, x) == x - 1:
|
||||
print()
|
||||
|
||||
from time import sleep
|
||||
def find_largest_square(board, prep_board, x, full):
|
||||
idx = 0
|
||||
idx_of_square = 0
|
||||
size = 0
|
||||
while idx < len(prep_board):
|
||||
print()
|
||||
print(f"{idx=}")
|
||||
print_prep_board_idx(prep_board, idx, x, full)
|
||||
print_board_idx(board, idx, x, full)
|
||||
while count_obst(prep_board, size, idx, x) == 0:
|
||||
print("YES")
|
||||
idx_of_square = idx
|
||||
size += 1
|
||||
sleep(.1)
|
||||
print()
|
||||
print(f"{idx=}")
|
||||
print_prep_board_idx(prep_board, idx, x, full)
|
||||
print_board_idx(board, idx, x, full)
|
||||
idx += 1
|
||||
sleep(.1)
|
||||
size -= 1
|
||||
return idx_of_square, size
|
||||
|
||||
def is_in_square(idx, sol_idx, sq_size, x):
|
||||
i = to_i(idx, x)
|
||||
j = to_j(idx, x)
|
||||
sol_i = to_i(sol_idx, x)
|
||||
sol_j = to_j(sol_idx, x)
|
||||
if sol_i < i <= sol_i + sq_size - 1:
|
||||
if sol_j < j <= sol_j + sq_size - 1:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def print_sol(board, sol_idx, sq_size, x, full):
|
||||
for idx in range(len(board)):
|
||||
if is_in_square(idx, sol_idx, sq_size, x):
|
||||
print(board[idx], end='')
|
||||
else:
|
||||
print(end=full)
|
||||
if to_j(idx, x) == x - 1:
|
||||
print()
|
||||
|
||||
def main():
|
||||
with open('map2') as m:
|
||||
inp = m.read().splitlines()
|
||||
first_line = inp[0]
|
||||
board = ''
|
||||
empty = first_line[-3]
|
||||
obst = first_line[-2]
|
||||
full = first_line[-1]
|
||||
y = int(first_line[:-3])
|
||||
x = len(inp[1])
|
||||
for line in inp[1:]:
|
||||
print(line)
|
||||
board += line
|
||||
board = list(board)
|
||||
prep_board = board.copy()
|
||||
for idx in range(x * y):
|
||||
i = to_i(idx, x)
|
||||
j = to_j(idx, x)
|
||||
if i > 0 and j > 0:
|
||||
prep_board[idx] = is_obst(board, idx, obst) + \
|
||||
prep_board[plus_j(idx, -1, len(prep_board))] + \
|
||||
prep_board[plus_i(idx, x, -1, len(prep_board))] - \
|
||||
prep_board[plus_i(plus_j(idx, -1, len(prep_board)), x, -1, len(prep_board))]
|
||||
elif i > 0:
|
||||
prep_board[idx] = is_obst(board, idx, obst) + \
|
||||
prep_board[plus_i(idx, x, -1, len(prep_board))]
|
||||
elif j > 0:
|
||||
prep_board[idx] = is_obst(board, idx, obst) + \
|
||||
prep_board[plus_j(idx, -1, len(prep_board))]
|
||||
else:
|
||||
prep_board[idx] = is_obst(board, idx, obst)
|
||||
# for i in range(y):
|
||||
# for j in range(x):
|
||||
# print(to_char(prep_board[j + i * x]), end=' ')
|
||||
# print()
|
||||
print()
|
||||
idx, size = find_largest_square(board, prep_board, x, full)
|
||||
print_sol(board, idx, size, x, full)
|
||||
print(idx, size)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
Loading…
Reference in New Issue