129 lines
3.2 KiB
Python
129 lines
3.2 KiB
Python
import time
|
|
global l
|
|
l = ["....#.....",
|
|
".........#",
|
|
"..........",
|
|
"..#.......",
|
|
".......#..",
|
|
"..........",
|
|
".#..^.....",
|
|
"........#.",
|
|
"#.........",
|
|
"......#..."]
|
|
t0 = time.time()
|
|
# with open('day6.txt') as f:
|
|
# l = f.read().splitlines()
|
|
|
|
global up, down, left, right
|
|
up,down,left,right = "^","v","<",">"
|
|
dir = [up,down,left,right]
|
|
global act_dir
|
|
act_dir = (0,0)
|
|
global obstacles
|
|
obstacles = []
|
|
pose = None
|
|
global path
|
|
path = []
|
|
global h,w
|
|
h,w = len(l),len(l[0])
|
|
global init_pose
|
|
global init_dir
|
|
global loops
|
|
loops = 0
|
|
|
|
|
|
def move_up(pose):
|
|
# print(path)
|
|
out_of_map = False
|
|
while (pose[0] - 1,pose[1]) not in obstacles: #or pose[0] != 0:
|
|
# print(pose, path)
|
|
pose = (pose[0] - 1,pose[1])
|
|
if pose not in path:
|
|
path.append(pose)
|
|
if pose[0] == 0:
|
|
out_of_map = True
|
|
break
|
|
if act_dir == init_dir and pose == init_pose:
|
|
loop = True
|
|
# print(pose)
|
|
# print(path)
|
|
act_dir = right
|
|
return pose, out_of_map, loop
|
|
|
|
def move_down(pose):
|
|
out_of_map = False
|
|
loop = False
|
|
while (pose[0] + 1,pose[1]) not in obstacles: #or pose[0] == h-1:
|
|
pose = (pose[0] + 1,pose[1])
|
|
# print(pose in path)
|
|
if pose not in path:
|
|
path.append(pose)
|
|
if pose[0] == h-1:
|
|
out_of_map = True
|
|
break
|
|
if act_dir == init_dir and pose == init_pose:
|
|
loop = True
|
|
act_dir = left
|
|
return pose, out_of_map, loop
|
|
|
|
def move_left(pose):
|
|
out_of_map = False
|
|
while (pose[0],pose[1] - 1) not in obstacles or pose[1] == 0:
|
|
pose = (pose[0],pose[1] - 1)
|
|
if pose not in path:
|
|
path.append(pose)
|
|
if pose[1] == 0:
|
|
out_of_map = True
|
|
break
|
|
if act_dir == init_dir and pose == init_pose:
|
|
loop = True
|
|
act_dir = up
|
|
return pose, out_of_map, loop
|
|
|
|
def move_right(pose):
|
|
out_of_map = False
|
|
while (pose[0],pose[1] + 1) not in obstacles: #or pose[1] == w - 1:
|
|
pose = (pose[0],pose[1] + 1)
|
|
if pose not in path:
|
|
path.append(pose)
|
|
if pose[1] == w - 1:
|
|
out_of_map = True
|
|
break
|
|
if act_dir == init_dir and pose == init_pose:
|
|
loop = True
|
|
act_dir = down
|
|
return pose, out_of_map, loop
|
|
|
|
|
|
for i in range(len(l)):
|
|
for j in range(len(l[0])):
|
|
if l[i][j] in dir:
|
|
act_dir = l[i][j]
|
|
init_pose, init_dir = (i,j), l[i][j]
|
|
pose = (i,j)
|
|
path.append(pose)
|
|
elif l[i][j] == "#":
|
|
obstacles.append((i,j))
|
|
# print(obstacles)
|
|
# print(path)
|
|
|
|
out_of_map = False
|
|
for i in range(h):
|
|
for j in range(w):
|
|
if (i,j) not in obstacles:
|
|
obstacles.append((i,j))
|
|
while not out_of_map:
|
|
if act_dir == up:
|
|
pose, out_of_map, loop = move_up(pose)
|
|
elif act_dir == down:
|
|
pose, out_of_map, loop = move_down(pose)
|
|
elif act_dir == left:
|
|
pose, out_of_map, loop = move_left(pose)
|
|
elif act_dir == right:
|
|
pose, out_of_map, loop = move_right(pose)
|
|
if loop :
|
|
loops +=1
|
|
|
|
print(len(path))
|
|
print(time.time()- t0)
|