Advent of Code 2021 - Day 22

preview_player
Показать описание
In this video series, I try to challenge myself with the Advent of Code trials. Each solution will be published to Github, and I hope you will learn something from my coding mistakes and perhaps send some code my way on how you have done these challenges. I know by reading code, so this is such an exciting thing for me.

GitHub repository:

Unlock unlimited opportunities with 50% off your first month of Coursera Plus

Get on the fast track to a career in cybersecurity. In this certificate program, you'll learn in-demand skills, and get AI training from Google experts. Learn at your own pace, no degree or experience required.

Join the channel to get access to more perks:

Or visit my blog at:

Outro music: Sanaas Scylla

#aoc2021 #advent #code
Рекомендации по теме
Комментарии
Автор

I've used similar approach. but turns out you do not need to sort cubes by size, or have a merge algorithm or track which ones are split or not.

1. Add first cuboid from the unprocessed list to the processed list.
2. Take another cuboid from the unprocessed list. Check whether it intersects with the first one. If it does. then replace it in the processed list with cubes you get by splitting the original cube with the _intersecting_ part. E.g. you have [10..12, 10..12, 10..12] and [9..11, 11..11, 11..11]. Then the intersecting part is [10..11, 11..11, 11..11]. First cube gets split into [12..12, 10..12, 10..12], [10..11, 10..10, 10..12], [10..11, 12..12, 10..12], [10..11, 10..10, 10..10] and [10..11, 10..10, 12..12]. _Do not include the intersecting part_.
Now: if the cuboid you took from the unprocessed list (the second cuboid) is "on", then add it to the processed list. If it is off, then move to the next step. Doing this actually does the "merging" for free. Think of it: intersecting cuboid of any smaller cubes that fit entirely into the cuboid will provide themselves as the intersecting part. And we do not add the intersecting parts. So effectively they get replaced by the larger cuboid. And if it is on, then that part of the grid will be on. If the large cuboid is "off", tnen the space taken up by smaller cuboids before will be off, because those cuboids were removed, and nothing new was added.
3 If there are cuboids in the unprocessed list, go to step 2
4. add up the volumes for cuboids in the processed list. Voila.

Alas, there is even simpler solution, you do not actually need to do the splitting, just keep track of the intersecting areas :(

RimantasLiubertas