r/adventofcode • u/daggerdragon • Dec 03 '20
SOLUTION MEGATHREAD -🎄- 2020 Day 03 Solutions -🎄-
Advent of Code 2020: Gettin' Crafty With It
- T-3 days until unlock!
- Full details and rules are in the Submissions Megathread
--- Day 03: Toboggan Trajectory ---
Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, 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 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:04:56, megathread unlocked!
90
Upvotes
View all comments
Show parent comment
1
u/jonathan_paulson Dec 04 '20
Here's another way to think about repeating the same pattern to the right - it's the same as just wrapping back around to the left. So instead of imagining expanding the grid, we can just imagine connecting the right side of the grid back to the left side. You could even imagine doing this physically; if you had the grid printed out on a sheet of paper, you could fold the right side back over and glue it to the left side, creating a cylinder. As you scanned the cylinder from left to right, you would encounter the desired infinitely repeating pattern, but unlike the "expand the grid" solution, the cylinder wouldn't be any bigger than the original grid (and unlike the "expand the grid" solution, this actually achieves infinite repeating without requiring infinite space).
Modulo is a very natural way of implementing this "wrap around" behavior. That's one to think about how modulo works - if you count modulo N that just means that whenever you get to N you wrap back to 0. You could think of the numbers modulo N as that the same cylinder with the numbers `0...N-1`; you can keep going forever and it will keep repeating this pattern. You could think about my column index as being on a cylinder instead of a grid, and the modulo is resetting it back to 0 each time it wraps around the cylinder.
Here's an example of another problem that uses modulo in this way: https://youtu.be/k5ruLG0lk8E?t=1101 (problem statement: https://atcoder.jp/contests/arc109/tasks/arc109_c). It's significantly harder than this problem though.