Skip to content

Commit

Permalink
Extract dict as constant
Browse files Browse the repository at this point in the history
  • Loading branch information
loociano committed Dec 15, 2024
1 parent ad47eb9 commit 292ced7
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions aoc2024/src/day15/python/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,12 @@
_BOX = 'O'
_WALL = '#'
_EMPTY = '.'


def _opposite_dir(dir: Direction) -> Direction:
inverse = {
(0, 1): (0, -1),
(0, -1): (0, 1),
(-1, 0): (1, 0),
(1, 0): (-1, 0),
}
return inverse.get(dir)
_DIR_TO_OPPOSITE = {
(0, 1): (0, -1), # Right to left.
(0, -1): (0, 1), # Left to right.
(-1, 0): (1, 0), # Up to down.
(1, 0): (-1, 0), # Down to up.
}


class Warehouse:
Expand Down Expand Up @@ -113,7 +109,7 @@ def _push(self, pos: Position, dir: Direction) -> None:
next_pos = next_empty_pos
while next_pos != pos:
self._update(next_pos, _BOX)
opposite_dir = _opposite_dir(dir)
opposite_dir = _DIR_TO_OPPOSITE.get(dir)
next_pos = (next_pos[0] + opposite_dir[0], next_pos[1] + opposite_dir[1])
self._update(pos, _EMPTY)

Expand Down

0 comments on commit 292ced7

Please sign in to comment.