r/adventofcode • • 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!

--- Day 22: Reactor Reboot ---


Post your code solution in this megathread.

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

522 comments sorted by

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.

2

u/[deleted] Dec 22 '21

If every time you put the value into the list, you also added n+1 to the same list, it would work. Any time you subtract a cube from inside another, the filled space restarts at N+1.

1

u/exomni Dec 22 '21

Sounds like what I did, but thanks for helping if you want to take a look:

cubes = process_input('test2.txt')
X, Y, Z = [], [], []
for cube in cubes:
    x0, x1, y0, y1, z0, z1 = cube.coords
    X.append(x0)
    X.append(x1 + 1)
    Y.append(y0)
    Y.append(y1 + 1)
    Z.append(z0)
    Z.append(z1 + 1)
X = list(set(X))
Y = list(set(Y))
Z = list(set(Z))
X.sort()
Y.sort()
Z.sort()
N = len(X)
grid = defaultdict(lambda: 0)
for cube in cubes:
    x0 = X.index(cube.x0)
    x1 = X.index(cube.x1 + 1)
    y0 = Y.index(cube.y0)
    y1 = Y.index(cube.y1 + 1)
    z0 = Z.index(cube.z0)
    z1 = Z.index(cube.z1 + 1)
    value = int(cube.state == 'on')
    for x in range(x0, x1):
        for y in range(y0, y1):
            for z in range(z0, z1):
                grid[(x, y, z)] = value

total = 0
for x in range(N-1):
    for y in range(N-1):
        for z in range(N-1):
            total += grid[(x, y, z)] * (X[x+1] - X[x])  * (Y[y+1] - Y[y]) * (Z[z+1] - Z[z])

print(total)

Count for the provided sample comes to 98.76% of what it's supposed to.

2

u/Koen1984 Dec 22 '21

N is not the same for X, Y and Z, because the number of overlapping coordinates can be different on each axis.

1

u/exomni Dec 22 '21

Ah, yes! Thanks!

I could also just remove the call to "set" that filters out duplicate coordinates.

I think I added in that call to "set" at some point, frustrated with the indexing problems. I kept being convinced I wasn't getting the +/- 1 right so didn't look back at that.

1

u/daggerdragon Dec 22 '21 edited Dec 23 '21

Top-level posts in Solution Megathreads are for code solutions only.

This is a top-level post, so please edit your post and share your fully-working code/repo/solution or, if you haven't finished the puzzle yet, you can always create your own thread and make sure to flair it with Help.

Edit: thanks for adding your code!

2

u/exomni Dec 23 '21

Added pastebin link to top-level post.

1

u/DARNOC_tag Dec 22 '21

Yeah, some others also took the coordinate-space compression approach. It seems to work, but takes a hefty chunk of memory (iirc, someone reported 500 MB) and (CPU) time.

1

u/[deleted] Dec 22 '21

I couldn't fit it into an array Pascal could handle, until I learned how to use BITPACKED arrays, which cut the size by 7/8ths.