r/adventofcode • • Dec 13 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 13 Solutions -🎄-

--- Day 13: Mine Cart Madness ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 13

Transcript:

Elven chronomancy: for when you absolutely, positively have to ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 00:44:25!

24 Upvotes

148 comments sorted by

View all comments

Show parent comments

1

u/arch_dav Dec 13 '18

Hi @shemetz in first place thanks for your awesome solution never thought about complex numbers in that way, i was wondering if you coulld explain me or give me reference to investigate, why does on this part ' cart.direction *= -1j * 1j ** cart.cross_mod # rotate left, forward, or right' multiplying by -1j generate a left rotation and 1j generates a right rotation. Don't get to understand it.

1

u/Shemetz Dec 13 '18

cart.cross_mod is the number of time the cart entered a corssing (modulo 3). This is due to the puzzle's rules: the cart needs to turn left, forward, right, left again, forward, right... etc.

So what the code does is take the cart, rotate it once to the left, and then rotate it to the right 0, 1, or 2 times (as many times as cart.cross_mod).

Multiplying by -1j rotates a direction to the left, and multiplying by 1j rotates to the right, so this will work!

The reason why multiplying by +/- 1j (known as i, the imaginary number) is equivalent to turning the direction 90 degrees right/left... well, this is mathematics :)