Broken Python load_asteroid_graph fix

This commit is contained in:
Tomáš Sláma 2025-02-19 11:14:27 +01:00
parent 616c1edf04
commit d786d39d3c

View file

@ -2,37 +2,36 @@ from asteracer import *
def load_asteroid_graph(path: str): def load_asteroid_graph(path: str):
with open(path) as f: with open(path) as file:
contents = [ lines = [line.strip() for line in file.readlines()]
line
for line in f.read().splitlines()
if not line.startswith("#") and line.strip() != ""
]
n_racer, n_asteroid, n_goal, m = list(map(int, contents[0].split())) # Filter out comments and empty lines
contents = [line for line in lines if line and not line.startswith('#')]
vertices = [] iter_lines = iter(contents)
edges = [] first_line = list(map(int, next(iter_lines).split()))
vertex_objects = [] n_racer, n_asteroid, n_goal, m = first_line
# Load vertices vertices = []
for i in range(1, 1 + n_racer + n_asteroid + n_goal): edges = []
line = contents[i + 1].split() vertex_objects = []
vertices.append((int(line[0]), int(line[1]))) # Load vertices
for i in range(n_racer + n_asteroid + n_goal):
line = list(map(int, next(iter_lines).split()))
vertices.append((line[0], line[1]))
if 0 <= i < n_racer: if i < n_racer:
vertex_objects.append(("S", i)) vertex_objects.append(('S', i))
elif n_racer <= i < n_asteroid: elif i < (n_racer + n_asteroid):
vertex_objects.append(("A", int(line[2]))) vertex_objects.append(('A', line[2]))
elif n_asteroid <= i < n_goal: else:
vertex_objects.append(("G", int(line[2]))) vertex_objects.append(('G', line[2]))
# Load edges # Load edges
for i in range(i, i + m): for _ in range(m):
line = contents[i + 1].split() line = list(map(int, next(iter_lines).split()))
edges.append((line[0], line[1]))
edges.append((int(line[0]), int(line[1])))
return vertices, edges, vertex_objects return vertices, edges, vertex_objects