It’s happening already. The slow crushing creep of laziness. I have not done part 2, yet, and I will, maybe get to it. I have ideas for how to solve part 2, its just… Kind of tedious and I want a respite.
Part 1 was fun though. Basically just, a simple pathfinding robot to trace the path of a guard through a maze of obstacles. I even gave it the ability to draw the map, but the dataset is bigger than my monitor so the refresh and drawing it’s stupid ugly with the real data set. I also turned it off because it drags the entire process to a crawl. A seconds-long calculation takes minutes and counting when its drawing nonsense.
The sample dataset looks neat though when drawn.
Part 2 is to find every place you could drop an object to create a loop for the guard. I thought I had it, I had the right idea, but I only ended up finding all of the existing loops. And I am not sure some were not in the middle of “walls”.
I got to thinking about what I did wrong, which is basically, checking for squares (loops) instead of finding them. I got to thinking about new algorithms to check all directions instead of just one, but what I need to do is to check along each side for walls and see if the gap is longer than the previous sides. But that’s not going to work exactly, so I think I can use the distances traveled to see if there are places to drop objects based on if sides are shorter than the previous sides.
That probably made more sense in my head.
It probably isn’t that hard, I just, don’t feel like doing it right now. Occasionally there is a stupid simple one thrown in that takes 5 minutes to do both parts, maybe I can come back to today on that day.
Or maybe just sometime tomorrow, tomorrow is surprisingly pretty open.
Anyway, here is the code, I trimmed out the useless part 2 function.
import time
import os
with open("Day06Input.txt") as file:
data = file.read()
lines = data.split("\n")
lines.pop()
guard = [0,0,0]
grid = []
guard_active = True
total = 0
total2 = 0
distances = []
cur_steps = 0
for each in lines:
if "^" in each:
guard[0] = each.index("^")
guard[1] = lines.index(each)
# print(guard)
grid.append(list(each))
def print_grid(thegrid):
for each in thegrid:
print("".join(each))
#print_grid(grid)
def move_guard(thegrid, guard_pos):
# 0 - North, 1 - East, 2 - South, 3 - West
# Defind in guard[2]
# guard[0] = x coord (across rows), guard [1] = y coord (up and down lines)
step = 1
if (thegrid[guard_pos[1]-1][guard_pos[0]] != "#") and guard_pos[2] == 0:
thegrid[guard_pos[1]][guard_pos[0]] = "X"
thegrid[guard_pos[1]-1][guard_pos[0]] = "^"
guard_pos[1] = guard_pos[1]-1
elif (thegrid[guard_pos[1]][guard_pos[0]+1] != "#") and guard_pos[2] == 1:
thegrid[guard_pos[1]][guard_pos[0]] = "X"
thegrid[guard_pos[1]][guard_pos[0]+1] = "^"
guard_pos[0] = guard_pos[0]+1
elif (thegrid[guard_pos[1]+1][guard_pos[0]] != "#") and guard_pos[2] == 2:
thegrid[guard_pos[1]][guard_pos[0]] = "X"
thegrid[guard_pos[1]+1][guard_pos[0]] = "^"
guard_pos[1] = guard_pos[1]+1
elif (thegrid[guard_pos[1]][guard_pos[0]-1] != "#") and guard_pos[2] == 3:
thegrid[guard_pos[1]][guard_pos[0]] = "X"
thegrid[guard_pos[1]][guard_pos[0]-1] = "^"
guard_pos[0] = guard_pos[0]-1
else:
guard_pos[2] = (guard_pos[2]+1) % 4
step = 0
# print(guard_pos)
return thegrid, guard_pos, step
while guard_active:
this_step = 0
grid, guard, this_step = move_guard(grid, guard)
if this_step == 0:
distances.append(cur_steps)
cur_steps = 0
else:
cur_steps += this_step
# Optionally Print the Map
# print_grid(grid)
# time.sleep(.001)
# os.system('clear')
# print(guard)
# print(len(grid)-1)
if guard[1] >= len(grid)-1 or guard[1] <= 0 or guard[0] >= len(grid[0])-1 or guard[0] <= 0:
grid[guard[1]][guard[0]] = "X"
guard_active = False
print("The guard has left the area!")
for each in grid:
total+= each.count("X")
#Print the final grid for fun
#print_grid(grid)
print(distances)
print(total)
# 5318
print(total2)
Josh Miller aka “Ramen Junkie”. I write about my various hobbies here. Mostly coding, photography, and music. Sometimes I just write about life in general. I also post sometimes about toy collecting and video games at Lameazoid.com.
Comments are closed.
Mentions