Skip to content

Commit

Permalink
aoc(2024-06): 🎄
Browse files Browse the repository at this point in the history
  • Loading branch information
JeeZeh committed Dec 7, 2024
1 parent 9c0f13c commit 3b1060d
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions 2024-py/advent/solutions/day06.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def find_guard(self, puzzle_input: str):
raise ValueError("No guard found")

def patrol(self, floor: Grid[Tile], guard: Pos, facing: int):
guard = guard
while True:
yield guard, facing
step = guard.add(DIRECTIONS[facing].value)
Expand All @@ -62,16 +61,13 @@ def loops(self, floor: Grid[Tile], guard: Pos, facing: int):
turtle_generator = self.patrol(floor, guard, facing)
hare_generator = self.patrol(floor, guard, facing)

turtle = next(turtle_generator)
hare = next(hare_generator)
next(hare_generator)
try:
while True:
if hare == turtle:
return True
next(hare_generator)
hare = next(hare_generator)
turtle = next(turtle_generator)
if hare == turtle:
return True
except StopIteration:
return False

Expand All @@ -80,20 +76,19 @@ def run(self, puzzle_input: str):
guard = self.find_guard(puzzle_input)

start = guard.add(Direction.UP.value)
walk: list[tuple[Pos, int]] = list(self.patrol(floor, guard, DIRECTIONS.index(Direction.UP)))
seen: set[tuple[Pos, int]] = {(start, 0)}
walk = len({pos for pos, _ in self.patrol(floor, guard, DIRECTIONS.index(Direction.UP))})
looping_blocks: list[Pos] = []
for walking, facing in walk:
seen: set[Pos] = set()
for walking, _ in self.patrol(floor, guard, DIRECTIONS.index(Direction.UP)):
# Place block
place_at = walking.add(DIRECTIONS[facing].value)
if floor.get(place_at) == Tile.SPACE and (place_at, facing) not in seen:
if walking != start and floor.get(walking) == Tile.SPACE and walking not in seen:
new_floor = deepcopy(floor)
new_floor.grid[place_at.y][place_at.x] = Tile.BLOCK
if self.loops(new_floor, walking, facing):
looping_blocks.append(place_at)
seen.add((place_at, facing))
new_floor.grid[walking.y][walking.x] = Tile.BLOCK
if self.loops(new_floor, guard, 0):
looping_blocks.append(walking)
seen.add(walking)

return len(set(pos for pos, _ in walk)), len(set(looping_blocks))
return walk, len(set(looping_blocks))


if __name__ == "__main__":
Expand Down

0 comments on commit 3b1060d

Please sign in to comment.