Skip to content

Commit

Permalink
Tidy up 2023/16 logic
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacG committed Dec 16, 2023
1 parent aaa6995 commit 9d83922
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
45 changes: 25 additions & 20 deletions 2023/16.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,35 @@ def energized(self, parsed_input: InputType, start_pos, start_dir) -> int:
if (char := parsed_input.get(pos, None)) is None:
# Off the map.
pass
elif (
char == "."
or (char == "|" and direction in (UP, DOWN))
or (char == "-" and direction in (RIGHT, LEFT))
):
elif char == ".":
# Pass through empty space and pointy side of splitters.
new_beams.add((pos, direction))
elif char == "|":
# Split.
new_beams.add((pos, UP))
new_beams.add((pos, DOWN))
if direction in (UP, DOWN):
new_beams.add((pos, direction))
else:
# Split.
new_beams.add((pos, UP))
new_beams.add((pos, DOWN))
elif char == "-":
# Split.
new_beams.add((pos, RIGHT))
new_beams.add((pos, LEFT))
elif (
(char == "/" and direction in (RIGHT, LEFT))
or (char == "\\" and direction in (UP, DOWN))
):
# Rotate counter-clockwise.
new_beams.add((pos, direction * -1j))
else:
# Rotate clockwise.
new_beams.add((pos, direction * 1j))
if direction in (RIGHT, LEFT):
new_beams.add((pos, direction))
else:
# Split.
new_beams.add((pos, RIGHT))
new_beams.add((pos, LEFT))
elif char == "/":
# Rotate.
if direction in (RIGHT, LEFT):
new_beams.add((pos, direction * -1j))
else:
new_beams.add((pos, direction * +1j))
elif char == "\\":
# Rotate.
if direction in (RIGHT, LEFT):
new_beams.add((pos, direction * +1j))
else:
new_beams.add((pos, direction * -1j))
# Ignore already-handled beams (loop detection).
beams = new_beams - seen
seen.update(new_beams)
Expand Down
14 changes: 14 additions & 0 deletions 2023/notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,17 @@ My part 1 solution was decently fast to pass the example.
However, it took me the longest time to figure out why it failed the real input.
I seeded my data with a beam at `(0, 0) RIGHT` then loop where I examine the next time.
This was fine in the example with `(0, 0)` is empty but the real data has a reflector at `(0, 0)` which I skipped.
The massive logic block could be changed to a dictionary lookup but that bumps runtime from 8s to 11s.
```python
# Out direction(s) based on in direction.
DIR_IN_TO_OUT = {
"#": {direction: {} for direction in aoc.FOUR_DIRECTIONS},
".": {direction: {direction} for direction in aoc.FOUR_DIRECTIONS},
"|": {UP: {UP}, DOWN: {DOWN}, RIGHT: {UP, DOWN}, LEFT: {UP, DOWN}},
"-": {RIGHT: {RIGHT}, LEFT: {LEFT}, UP: {RIGHT, LEFT}, DOWN: {RIGHT, LEFT}},
"/": {RIGHT: {UP}, LEFT: {DOWN}, UP: {RIGHT}, DOWN: {LEFT}},
"\\": {RIGHT: {DOWN}, LEFT: {UP}, UP: {LEFT}, DOWN: {RIGHT}},
}
```

0 comments on commit 9d83922

Please sign in to comment.