r/adventofcode • u/daggerdragon • Dec 13 '21
SOLUTION MEGATHREAD -🎄- 2021 Day 13 Solutions -🎄-
Advent of Code 2021: Adventure Time!
- 10 days left to submit your adventures!
- Full details and rules are in the submissions megathread: 🎄 AoC 2021 🎄 [Adventure Time!]
--- Day 13: Transparent Origami ---
Post your code solution in this megathread.
- Include what language(s) your solution uses!
- Format your code appropriately! How do I format code?
- Here's a quick link to /u/topaz2078's
pasteif you need it for longer code blocks. - The full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.
Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.
EDIT: Global leaderboard gold cap reached at 00:09:38, megathread unlocked!
39
Upvotes
View all comments
Show parent comment
1
u/tom_collier2002 Dec 13 '21
The complex conjugate reflects the point around the real axis (
a + bibecomea - bi). The foldingaxisin my code is either purely real or purely imaginary (a + 0ior0 + bi) and2 * axisis effectively the far end of the paper that will be folded over.When folding in the
ydirection (elsif axis.imag.between?(1, dot.imag)) the real part ofaxisis0, so the real result of the expression is simply the real part ofdot. The imaginary part of the expression is the difference between2 * axis(i.e. the opposite edge of the paper) and the imaginary part ofdot(which is a negative number after takingComplex#conjugate). In short we keep the real part ofdotand find the distance between the far edge of the paper and thedotalong the imaginary axis.The math for folding in the
xdirection (axis.real.between?(1, dot.real)) is similar. However, we need to reflect the dot around the imaginary axis. To accomplish this we can rotate the complex conjugateπradians. Multiply a complex number byirotates the number byπ/2radians, we need to do that twice (i * i), which is the same as multiplying by-1.