From d786d39d3c81463c2d448541625adb56eea7c6fb Mon Sep 17 00:00:00 2001 From: xiaoxiae <tomas@slama.dev> Date: Wed, 19 Feb 2025 11:14:27 +0100 Subject: [PATCH] Broken Python load_asteroid_graph fix --- asteracer-python/__main__.py | 49 ++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/asteracer-python/__main__.py b/asteracer-python/__main__.py index d3bb215..375414a 100644 --- a/asteracer-python/__main__.py +++ b/asteracer-python/__main__.py @@ -2,37 +2,36 @@ from asteracer import * def load_asteroid_graph(path: str): - with open(path) as f: - contents = [ - line - for line in f.read().splitlines() - if not line.startswith("#") and line.strip() != "" - ] + with open(path) as file: + lines = [line.strip() for line in file.readlines()] - 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 = [] - edges = [] - vertex_objects = [] + iter_lines = iter(contents) + first_line = list(map(int, next(iter_lines).split())) + n_racer, n_asteroid, n_goal, m = first_line - # Load vertices - for i in range(1, 1 + n_racer + n_asteroid + n_goal): - line = contents[i + 1].split() + vertices = [] + edges = [] + 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: - vertex_objects.append(("S", i)) - elif n_racer <= i < n_asteroid: - vertex_objects.append(("A", int(line[2]))) - elif n_asteroid <= i < n_goal: - vertex_objects.append(("G", int(line[2]))) + if i < n_racer: + vertex_objects.append(('S', i)) + elif i < (n_racer + n_asteroid): + vertex_objects.append(('A', line[2])) + else: + vertex_objects.append(('G', line[2])) - # Load edges - for i in range(i, i + m): - line = contents[i + 1].split() - - edges.append((int(line[0]), int(line[1]))) + # Load edges + for _ in range(m): + line = list(map(int, next(iter_lines).split())) + edges.append((line[0], line[1])) return vertices, edges, vertex_objects