adventOfCode/12-rainRisk
2020-12-14 19:52:30 +00:00
..
go Code formatting 2020-12-14 19:52:30 +00:00
python Day 12, Python: yes, this is *totally* how type hinting works 2020-12-12 21:00:24 +00:00
0.gif Day 12: Add visualisations 2020-12-12 15:49:36 +00:00
1.gif Day 12: Add visualisations 2020-12-12 15:49:36 +00:00
info.json Day 12 (Python) 2020-12-12 14:12:42 +00:00
quadrants.png Day 12: part two explanation 2020-12-12 21:55:28 +00:00
README.md Day 12: part two explanation 2020-12-12 21:55:28 +00:00
rotation.png Day 12: part two explanation 2020-12-12 21:55:28 +00:00

Day 12: Rain Risk

Part two explanation

In order to rotate the waypoint around the boat, a system of quadrants is used.

Imagine the boat is on the origin of a set of axis. Using those axis, you can define a set of quadrants based on the values of x and y

Boat quadrants

If you have a set of coordinates that represent a waypoint's offset from the boat, you can determine what quadrant it lies in relative to the boat based on the parameters shown in the image.

Because we will only ever be asked to rotate the waypoint a multiple of 90 degrees around the boat, we can determine the number of the quadrant after the rotation has been done by dividing the number of degrees by 90 and adding or subtracting that value from the quadrant number depending on which way we're turning the waypoint.

If the quadrant number has gone above four or below zero, we can subtract or add four respectively in order to bring the quadrant number back within 1 <= x <= 4.

Next, we switch the x and y values around if the quadrants are diagonally opposite each other (so if the modulus of the difference between the old and new quadrants is not zero). This is to compensate for the fact that a vector is essentially vertically flipped when rotated 180 degrees.

Vector rotation

Finally, we can make the values for x and y positive and negative according to the rules for each quadrant. For example, if the new quadrant is quadrant number two, we have to make the x and y values positive or negative so that x >= 0 and y < 0 is true.

The better way to do this (for angles that aren't just multiples of 90) is to use a transformation matrix and trigonometry.

When I say "the better way", I mean the much better way.

Visualisation

Part one Part two
partOne gif partTwo gif

Script output
 python .\python\
AoC 2020: day 12 - Rain Risk
Python 3.8.5

Test cases
1.1 pass
2.1 pass

Answers
Part 1: 1645
Part 2: 35292

 go run .\go\
AoC 2020: day 12 - Rain Risk
Go go1.15.2

Test cases
1.1 pass
2.1 pass

Answers
Part 1: 1645
Part 2: 35292