Skip to content

Commit

Permalink
[2024/6] p2 loop check using a set instead of a list (5m30s -> 10s)
Browse files Browse the repository at this point in the history
  • Loading branch information
StarlitGhost committed Dec 6, 2024
1 parent 7c4dbe4 commit 637f02d
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion 2024/6/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def simulate(pos: tuple, dir_: Dir, grid: Grid) -> tuple[set, list, bool]:
visited.add(pos)
path = []
path.append((pos, dir_))
path_set = set()
path_set.add((pos, dir_))
loops = False

while grid.in_bounds(pos):
Expand All @@ -28,11 +30,13 @@ def simulate(pos: tuple, dir_: Dir, grid: Grid) -> tuple[set, list, bool]:
if path[-1][0] == pos:
path.append((pos, dir_))
path.append((pos, None))
elif (pos, dir_) in path:
path_set.add((pos, dir_))
elif (pos, dir_) in path_set:
loops = True
break
else:
path.append((pos, dir_))
path_set.add((pos, dir_))

return visited, path, loops

Expand Down

0 comments on commit 637f02d

Please sign in to comment.