2024(6): god finally

I hate my code, though.
This commit is contained in:
2024-12-28 23:07:51 +01:00
parent e28df05c1e
commit 242bacd75d
2 changed files with 26 additions and 20 deletions

View File

@ -19,45 +19,51 @@ position; see Pigeonhole principle).
if __name__ == "__main__":
m = Map()
m.load_map("testinput")
#m.load_map("input")
#m.load_map("testinput")
m.load_map("input")
# Prepare forked map for speed
#forked_map = m.copy()
forked_map = Map()
#forked_map = Map()
#forked_map.load_map("input")
forked_map.load_map("testinput")
begin_pos = m.pos
begin_direction = m.direction
#forked_map.load_map("testinput")
#begin_pos = m.pos
#begin_direction = m.direction
loop_obstacles = []
i = 0
while m.step():
print(i)
#print(i)
if verbose:
print(m.pos, m.direction)
# Create a copy
#forked_map = m.copy()
#forked_map = Map()
#forked_map.load_map("input")
forked_map = Map()
#forked_map.load_map("testinput")
forked_map.load_map("input")
#forked_map.map = m.map.copy()
print(f"Forking map, setting obstacle at {m.next_pos()}, resetting current {m.pos} to {m.map[m.pos]}")
forked_map.show()
forked_map.map[m.next_pos()] = Map.Tiles.OBSTACLE.value
forked_map.map[m.pos] = m.map[m.pos]
forked_map.pos = begin_pos
forked_map.direction = begin_direction
forked_map.trace = [begin_pos]
forked_map.show()
print(f"Forking at {forked_map.next_pos()}... ", end="")
#print(f"Forking map, setting obstacle at {m.next_pos()}, resetting current {m.pos} to {m.map[m.pos]}")
#forked_map.show()
next_pos = m.next_pos()
if next_pos in loop_obstacles or not forked_map.pos_in_map(next_pos):
print(f"Skip forking at {next_pos} as already in loop_obstacles or last.")
continue
forked_map.map[next_pos] = Map.Tiles.OBSTACLE.value
#forked_map.map[m.pos] = m.map[m.pos]
#forked_map.pos = begin_pos
#forked_map.direction = begin_direction
#forked_map.trace = [begin_pos]
#forked_map.show()
print(f"Forking at {next_pos}... ", end="")
try:
while forked_map.step():
continue
except Map.LoopException:
print("loop.")
loop_obstacles.append(m.next_pos())
#forked_map.show()
else:
print("no loop.")
i += 1
@ -66,4 +72,4 @@ if __name__ == "__main__":
#m.show()
#print(len(m.trace), m.trace)
#print(len(set(m.trace)))
print(len(loop_obstacles))
print(len(set(loop_obstacles)))