AoC 2020 D12: Rain Risk

Python | Problem statement | Source code | Tags: GeometryGrid walking

← Previous Back to AoC Index Next →

Part 1

Literally do what the problem says. Keep track of the ship's position and direction, and update them according to the instructions. There's one small detail though: I thought the direction could be any angle, but in fact it's always a multiple of 90 degrees. I used the common trick of representing directions as complex numbers, so that turning is just multiplying by j or -j. On the complex plane, 1 is east, j is north, -1 is west, and -j is south.

action = line[0]
amount = int(line[1:])
if action == "R":
cur_dir *= (-1j) ** (amount // 90)
elif action == "L":
cur_dir *= 1j ** (amount // 90)
elif action == "F":
cur_pos += cur_dir * amount
else:
cur_pos += shift_dir[action] * amount
python

Part 2

In part 1 some people may have hardcoded the 4 directions, which is what part 2 tries to prevent. Because the waypoint can be anywhere, we must implement proper rotation. Since we are already doing that, the only difference is the initial direction (10 + 1j instead of 1) and that N/S/E/W instructions affect the waypoint instead of the ship.

← Previous Back to AoC Index Next →