Working on 2023-12-10 p1

This commit is contained in:
FrederikBaerentsen 2023-12-11 13:50:58 +01:00
parent a6055b0f8f
commit ee28498264

View File

@ -8,19 +8,25 @@ grid = []
log = True log = True
def p(x): def p(x,steps):
global log global log
if log: if log:
for i in x: for idx,i in enumerate(x):
for j in i: for jdx,j in enumerate(i):
if j == 'S': if j == 'S':
print(colored(j,'red'),end='') print(colored(j,'red'),end='')
elif (idx,jdx) in steps:
print(colored(j,'green'),end='')
else: else:
print(j,end='') print(j,end='')
print() print()
def start_coords(grid):
for ydx,y in enumerate(grid):
for xdx,x in enumerate(y):
if x == 'S':
return (xdx,ydx)
with open(sys.argv[1]) as file: with open(sys.argv[1]) as file:
for line in file: for line in file:
@ -29,4 +35,119 @@ with open(sys.argv[1]) as file:
for ldx,line in enumerate(grid): for ldx,line in enumerate(grid):
grid[ldx] = line.translate(str.maketrans("-|F7LJ.", "─│┌┐└┘ ")) grid[ldx] = line.translate(str.maketrans("-|F7LJ.", "─│┌┐└┘ "))
p(grid)
steps = []
#p(grid,steps)
found = False
start = start_coords(grid)
cur = start
prev = cur
count = 0
def wall(loc):
return grid[loc[1]][loc[0]]
"""
steps = [(3,3),(2,3),(4,3),(3,2),(3,4)]
tmps = []
for i in steps:
tmps.append(i)
p(grid,tmps)
input()
exit()
"""
y = cur[0]
x = cur[1]
print('Right is ' + wall((y+1,x)) + grid[x][y+1],end='')
print((y+1,x))
print('Left is ' + wall((y-1,x)) + grid[x][y-1],end='')
print((y-1,x))
print('Up is ' + wall((y,x-1)) + grid[x-1][y],end='')
print((y,x-1))
print('Down is ' + wall((y,x+1)) + grid[x+1][y],end='')
print((y,x+1))
print()
p(grid,steps)
while not found:
y = cur[0]
x = cur[1]
sub_found = False
print('Start: ' + str(cur) + ' ' + wall(cur))
dirc = ''
print('Trying to find way')
print('Previous is ' + str(prev) + wall(prev))
if y+1 < len(grid[0]) and not sub_found:
# Right
new = (list(cur)[0]+1,list(cur)[1])
if grid[x][y+1] in ('','','') and new != prev:
print('Trying Right')
print('prev: ' + str(prev) + ' cur: ' + str(cur) + ' new: ' + str(new))
dirc = 'R'
if wall(cur) in ('','','','S'):
print('Went Right')
prev = (y,x)
y += 1
sub_found = True
else:
print('Cant go right cus ' + str(y) + ' < ' + str(len(grid)))
if y > 0 and not sub_found:
# Left
new = (list(cur)[0]-1,list(cur)[1])
if grid[x][y-1] in ('','','') and new != prev:
print('Trying Left')
print('prev: ' + str(prev) + ' cur: ' + str(cur) + ' new: ' + str(new))
dirc = 'L'
if wall(cur) in ('','','','S'):
print('Went Left')
y -= 1
sub_found = True
else:
print('Cant go left cus ' + str(y) + ' > ' + str(0))
if x > 0 and not sub_found:
# Up
new = (list(cur)[0],list(cur)[1]-1)
if grid[x-1][y] in ('','','') and new != prev:
print('Trying Up')
print('prev: ' + str(prev) + ' cur: ' + str(cur) + ' new: ' + str(new))
dirc = 'U'
if wall(cur) in ('','','','S'):
print('Went Up')
x -=1
sub_found = True
if x < len(grid) and not sub_found:
# Down
new = (list(cur)[0],list(cur)[1]+1)
if grid[x+1][y] in ('','','') and new != prev:
print('Trying Down')
print('prev: ' + str(prev) + ' cur: ' + str(cur) + ' new: ' + str(new))
dirc = 'D'
if wall(cur) in ('','','','S'):
print('Went Down')
x += 1
sub_found = True
cur = (y,x)
steps.append((x,y))
print('Going ' + dirc + ' to ' + str(cur) + ' ' + wall(cur))
print('Previous is ' + str(prev) + ' ' + wall(prev))
p(grid,steps)
print()
input()
count += 1
if cur == start:
found = True
print(count)