168 lines
5.3 KiB
Python
168 lines
5.3 KiB
Python
with open('puzzle.txt', 'r') as f:
|
|
lines = f.read().splitlines()
|
|
|
|
import time
|
|
import copy
|
|
|
|
guard_map = {}
|
|
|
|
guard_orientation = '^'
|
|
|
|
total_visits = 0
|
|
|
|
for line_index, line in enumerate(lines):
|
|
for column_index, column in enumerate(list(line)):
|
|
if line_index not in guard_map:
|
|
guard_map[line_index] = {}
|
|
|
|
if column_index not in guard_map[line_index]:
|
|
guard_map[line_index][column_index] = {
|
|
'value': line[column_index],
|
|
}
|
|
|
|
if guard_map[line_index][column_index]['value'] == '^':
|
|
guard_map[line_index][column_index]['value'] = 'X'
|
|
guard_initial_x = column_index
|
|
guard_initial_y = line_index
|
|
|
|
guard_x = guard_initial_x
|
|
guard_y = guard_initial_y
|
|
|
|
#print(guard_map)
|
|
|
|
while True:
|
|
guard_map[guard_y ][guard_x]['value'] = 'X'
|
|
|
|
try:
|
|
#map_string = ""
|
|
#for line in guard_map:
|
|
# for column in guard_map[line]:
|
|
# if column == guard_x and line == guard_y:
|
|
# map_string += guard_orientation
|
|
# else:
|
|
# map_string += guard_map[line][column]['value']
|
|
# map_string += "\n"
|
|
|
|
#print(map_string)
|
|
|
|
match guard_orientation:
|
|
case '^':
|
|
if guard_map[guard_y - 1][guard_x]['value'] == '#':
|
|
guard_orientation = '>'
|
|
|
|
else:
|
|
guard_y -= 1
|
|
|
|
case '>':
|
|
if guard_map[guard_y][guard_x + 1]['value'] == '#':
|
|
guard_orientation = 'v'
|
|
|
|
else:
|
|
guard_x += 1
|
|
|
|
case 'v':
|
|
if guard_map[guard_y + 1][guard_x]['value'] == '#':
|
|
guard_orientation = '<'
|
|
|
|
else:
|
|
guard_y += 1
|
|
|
|
case '<':
|
|
if guard_map[guard_y][guard_x - 1]['value'] == '#':
|
|
guard_orientation = '^'
|
|
|
|
else:
|
|
guard_x -= 1
|
|
|
|
except KeyError:
|
|
break
|
|
|
|
for line in guard_map:
|
|
for column in guard_map[line]:
|
|
if guard_map[line][column]['value'] == 'X':
|
|
total_visits += 1
|
|
|
|
total_obstacles = 0
|
|
|
|
obstacle_coords = []
|
|
|
|
for line in guard_map:
|
|
for column in guard_map[line]:
|
|
guard_x = guard_initial_x
|
|
guard_y = guard_initial_y
|
|
obstacle_encountered = 0
|
|
encounter_x = None
|
|
encounter_y = None
|
|
guard_orientation = '^'
|
|
init_time = time.time()
|
|
if guard_map[line][column]['value'] == 'X':
|
|
guard_map_copy = copy.deepcopy(guard_map)
|
|
guard_map_copy[line][column]['value'] = '#'
|
|
|
|
while True:
|
|
#map_string = ""
|
|
#for line_map in guard_map_copy:
|
|
# for column_map in guard_map_copy[line_map]:
|
|
# if column_map == guard_x and line_map == guard_y:
|
|
# map_string += guard_orientation
|
|
# else:
|
|
# map_string += guard_map[line_map][column_map]['value']
|
|
# map_string += "\n"
|
|
|
|
#print(map_string)
|
|
guard_prev_orientation = guard_orientation
|
|
try:
|
|
match guard_orientation:
|
|
case '^':
|
|
if guard_map_copy[guard_y - 1][guard_x]['value'] == '#':
|
|
guard_orientation = '>'
|
|
|
|
else:
|
|
guard_y -= 1
|
|
|
|
case '>':
|
|
if guard_map_copy[guard_y][guard_x + 1]['value'] == '#':
|
|
guard_orientation = 'v'
|
|
|
|
else:
|
|
guard_x += 1
|
|
|
|
case 'v':
|
|
if guard_map_copy[guard_y + 1][guard_x]['value'] == '#':
|
|
guard_orientation = '<'
|
|
|
|
else:
|
|
guard_y += 1
|
|
|
|
case '<':
|
|
if guard_map_copy[guard_y][guard_x - 1]['value'] == '#':
|
|
guard_orientation = '^'
|
|
|
|
else:
|
|
guard_x -= 1
|
|
|
|
#if (abs(column - guard_x) == 1 and line - guard_y == 0) or (column - guard_x == 0 and abs(line - guard_y) == 1):
|
|
# obstacle_encountered += 1
|
|
# if obstacle_encountered == 1:
|
|
# encounter_orientation = guard_prev_orientation
|
|
# encounter_x = guard_x
|
|
# encounter_y = guard_y
|
|
#
|
|
#if (obstacle_encountered >= 2 and guard_x == encounter_x and guard_y == encounter_y and guard_prev_orientation == encounter_orientation):
|
|
# total_obstacles += 1
|
|
# print(line, column)
|
|
# obstacle_coords.append([line, column])
|
|
# print(total_obstacles)
|
|
# break
|
|
|
|
if time.time() - init_time > 0.1:
|
|
total_obstacles += 1
|
|
print(total_obstacles)
|
|
break
|
|
|
|
except KeyError:
|
|
break
|
|
|
|
print(total_visits)
|
|
print(total_obstacles)
|
|
print(obstacle_coords) |