Skip to content

Commit

Permalink
Merge pull request #216 from ephemient/py/day20
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient authored Jan 7, 2025
2 parents ca846f9 + 74e83d1 commit a67a47b
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions py/aoc2024/day20.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,26 @@

def _getpath(data: str) -> list[tuple[tuple[int, int], int]]:
data = data.splitlines()
stack = [
((y, x), {(y, x): 0})
pos = next(
(y, x)
for y, line in enumerate(data)
for x, char in enumerate(line)
if char == "S"
]
)
path = [(pos, 0)]
last = None
while True:
(y, x), path = stack.pop()
y, x = pos
if data[y][x] == "E":
return sorted(path.items())
for y, x in ((y - 1, x), (y, x - 1), (y, x + 1), (y + 1, x)):
if data[y][x] != "#" and (y, x) not in path:
stack.append(((y, x), path | {(y, x): len(path)}))
return sorted(path)
for pos2 in ((y - 1, x), (y, x - 1), (y, x + 1), (y + 1, x)):
y, x = pos2
if data[y][x] != "#" and pos2 != last:
path.append((pos2, len(path)))
last, pos = pos, pos2
break
else:
return None


def part1(data: str, time: int = 100) -> int:
Expand Down

0 comments on commit a67a47b

Please sign in to comment.