2023-12-11 14:31:41 +01:00
|
|
|
import sys
|
2023-12-11 20:36:17 +01:00
|
|
|
import os
|
2023-12-11 14:31:41 +01:00
|
|
|
from pprint import pprint
|
2023-12-11 20:36:17 +01:00
|
|
|
import time
|
|
|
|
|
|
|
|
from shapely.geometry import Point
|
|
|
|
from shapely.geometry.polygon import Polygon
|
2023-12-11 14:31:41 +01:00
|
|
|
|
|
|
|
#colors
|
|
|
|
from termcolor import colored
|
|
|
|
|
|
|
|
grid = []
|
2023-12-11 20:36:17 +01:00
|
|
|
cur = []
|
2023-12-11 14:31:41 +01:00
|
|
|
log = True
|
|
|
|
|
2023-12-11 20:36:17 +01:00
|
|
|
def p(x,steps,inside,*cur):
|
2023-12-11 14:31:41 +01:00
|
|
|
global log
|
|
|
|
if log:
|
|
|
|
for idx,i in enumerate(x):
|
|
|
|
for jdx,j in enumerate(i):
|
2023-12-11 20:36:17 +01:00
|
|
|
if len(cur) < 0:
|
|
|
|
if cur[1] == idx and cur[0] == jdx:
|
|
|
|
print(colored(j,'blue'),end='')
|
|
|
|
elif j == 'S':
|
2023-12-11 14:31:41 +01:00
|
|
|
print(colored(j,'red'),end='')
|
|
|
|
elif (idx,jdx) in steps:
|
|
|
|
print(colored(j,'green'),end='')
|
2023-12-11 20:36:17 +01:00
|
|
|
elif (idx,jdx) in inside:
|
|
|
|
print(colored(j,'yellow'),end='')
|
2023-12-11 14:31:41 +01:00
|
|
|
else:
|
|
|
|
print(j,end='')
|
|
|
|
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:
|
|
|
|
for line in file:
|
|
|
|
grid.append(line.rstrip())
|
|
|
|
|
|
|
|
for ldx,line in enumerate(grid):
|
2023-12-11 20:36:17 +01:00
|
|
|
grid[ldx] = line.translate(str.maketrans("-|F7LJ.", "─│┌┐└┘."))
|
2023-12-11 14:31:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
steps = []
|
2023-12-11 20:36:17 +01:00
|
|
|
inside = []
|
2023-12-11 14:31:41 +01:00
|
|
|
#p(grid,steps)
|
|
|
|
|
|
|
|
found = False
|
|
|
|
|
|
|
|
start = start_coords(grid)
|
|
|
|
cur = start
|
|
|
|
prev = cur
|
2023-12-11 21:19:18 +01:00
|
|
|
steps.append(start)
|
2023-12-11 14:31:41 +01:00
|
|
|
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()
|
2023-12-11 20:36:17 +01:00
|
|
|
p(grid,steps,inside)
|
2023-12-11 14:31:41 +01:00
|
|
|
|
|
|
|
while not found:
|
|
|
|
y = cur[0]
|
|
|
|
x = cur[1]
|
|
|
|
|
|
|
|
sub_found = False
|
|
|
|
#print('Start: ' + str(cur) + ' ' + wall(cur))
|
|
|
|
|
|
|
|
dirc = ''
|
2023-12-11 20:36:17 +01:00
|
|
|
if len(sys.argv) == 3:
|
|
|
|
p(grid,steps,inside)
|
|
|
|
time.sleep(float(sys.argv[2]))
|
2023-12-11 14:31:41 +01:00
|
|
|
|
|
|
|
#print('Trying to find way')
|
|
|
|
#print('Previous is ' + str(prev) + wall(prev))
|
|
|
|
if y+1 < len(grid[0]) and not sub_found:
|
|
|
|
#print('Can go right')
|
|
|
|
# Right
|
|
|
|
new = (list(cur)[0]+1,list(cur)[1])
|
|
|
|
#print(wall(new))
|
|
|
|
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'):
|
|
|
|
if wall(new) == 'S':
|
|
|
|
print('Found')
|
|
|
|
found = True
|
|
|
|
#print('Went Right')
|
|
|
|
prev = (y,x)
|
|
|
|
y += 1
|
|
|
|
#print(wall(new))
|
|
|
|
sub_found = True
|
|
|
|
|
|
|
|
if y > 0 and not sub_found:
|
|
|
|
#print('Can go left')
|
|
|
|
# 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'):
|
|
|
|
if wall(new) == 'S':
|
|
|
|
print('Found')
|
|
|
|
found = True
|
|
|
|
#print('Went Left')
|
|
|
|
prev = (y,x)
|
|
|
|
y -= 1
|
|
|
|
#print(wall(new))
|
|
|
|
sub_found = True
|
|
|
|
|
|
|
|
if x > 0 and not sub_found:
|
|
|
|
#print('Can go up')
|
|
|
|
# Up
|
|
|
|
new = (list(cur)[0],list(cur)[1]-1)
|
|
|
|
#print(wall(new))
|
|
|
|
if grid[x-1][y] in ('│','┌','┐','S') 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')
|
|
|
|
if wall(new) == 'S':
|
|
|
|
print('Found')
|
|
|
|
found = True
|
|
|
|
prev = (y,x)
|
|
|
|
x -=1
|
|
|
|
sub_found = True
|
|
|
|
|
|
|
|
if x < len(grid) and not sub_found:
|
|
|
|
#print('Can go down')
|
|
|
|
# Down
|
|
|
|
new = (list(cur)[0],list(cur)[1]+1)
|
|
|
|
#print(wall(new))
|
|
|
|
if grid[x+1][y] in ('│','└','┘','S') 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')
|
|
|
|
if wall(new) == 'S':
|
|
|
|
print('Found')
|
|
|
|
found = True
|
|
|
|
prev = (y,x)
|
|
|
|
x += 1
|
|
|
|
sub_found = True
|
|
|
|
|
|
|
|
|
|
|
|
cur = (y,x)
|
|
|
|
|
|
|
|
steps.append((x,y))
|
|
|
|
#p(grid,steps)
|
|
|
|
#print('Going ' + dirc + ' to ' + str(cur) + ' ' + wall(cur))
|
|
|
|
#print('Previous is ' + str(prev) + ' ' + wall(prev))
|
|
|
|
#print()
|
|
|
|
#input()
|
|
|
|
count += 1
|
|
|
|
#print(cur, start)
|
2023-12-11 20:36:17 +01:00
|
|
|
p(grid,steps,inside)
|
2023-12-11 14:31:41 +01:00
|
|
|
print(count)
|
|
|
|
print(count/2)
|
2023-12-11 21:19:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
# shapely doesn't give the right answer
|
|
|
|
"""
|
2023-12-11 20:36:17 +01:00
|
|
|
polygon = Polygon(steps)
|
|
|
|
|
|
|
|
for ydx,y in enumerate(grid):
|
|
|
|
for xdx,x in enumerate(y):
|
2023-12-11 21:19:18 +01:00
|
|
|
if (ydx,xdx) in steps:
|
|
|
|
continue
|
2023-12-11 20:36:17 +01:00
|
|
|
if polygon.contains(Point(ydx,xdx)):
|
|
|
|
inside.append((ydx,xdx))
|
|
|
|
if len(sys.argv) == 4:
|
|
|
|
p(grid,steps,inside,xdx,ydx)
|
|
|
|
time.sleep(float(sys.argv[3]))
|
|
|
|
if len(sys.argv) == 2:
|
|
|
|
p(grid,steps,inside)
|
|
|
|
print(len(inside))
|
2023-12-11 21:19:18 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
# but Mathplotlib does
|
|
|
|
|
|
|
|
from matplotlib.path import Path
|
|
|
|
inside = []
|
|
|
|
print(steps)
|
|
|
|
|
|
|
|
path = Path(steps)
|
|
|
|
|
|
|
|
for ydx,y in enumerate(grid):
|
|
|
|
for xdx,x in enumerate(y):
|
|
|
|
if (ydx,xdx) in steps:
|
|
|
|
continue
|
|
|
|
if path.contains_point((ydx,xdx)):
|
|
|
|
inside.append((ydx,xdx))
|
|
|
|
if len(sys.argv) == 4:
|
|
|
|
p(grid,steps,inside,xdx,ydx)
|
|
|
|
time.sleep(float(sys.argv[3]))
|
|
|
|
if len(sys.argv) == 2:
|
|
|
|
p(grid,steps,inside)
|
|
|
|
print(len(inside))
|
|
|
|
|
|
|
|
|
|
|
|
|