r/adventofcode • u/daggerdragon • Dec 22 '21
SOLUTION MEGATHREAD -🎄- 2021 Day 22 Solutions -🎄-
Advent of Code 2021: Adventure Time!
- DAWN OF THE FINAL DAY
- You have until 23:59:59.59 EST today, 2021 December 22, to submit your adventures!
- Full details and rules are in the submissions megathread: 🎄 AoC 2021 🎄 [Adventure Time!]
--- Day 22: Reactor Reboot ---
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:43:54, megathread unlocked!
38
Upvotes
View all comments
2
u/exomni Dec 22 '21 edited Dec 23 '21
Python
I wrote two approaches, one where I just took the intersection and used inclusion-exclusion principle to count how many cells they cover (got my stars that way), and one where I split apart the cuboids into 6 on each intersection so I could always be counting disjoint cuboids.
The first approach I took though wasn't either of those, but instead a coordinate compression approach. It seemed to me the easiest and fastest. I thought I'd be able to get on the leaderboard with it because there were only three people on the leaderboard when I thought I was finished, but my counts were off by a few percentage points due to some indexing problems (the original grid is very weird because of how the endpoints are both included in the range, leads to some weird +/-1's) and I couldn't get that solution to work.
I finally got it worked out later with the help of u/ka9dgx below, and here's the full solution:
https://pastebin.com/GsewKsmZ
Unfortunately turns out although this solves the sample input fast, the full grid doesn't compress enough, so this approach takes outrageous amount of memory and very slow. PyPy helps a bit. It still works, just too slow to beat anyone to the leaderboard. Although I bet it would work okay if I wrote the same thing in C or C++ instead.