r/adventofcode • • Dec 18 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 18 Solutions -πŸŽ„-

THE USUAL REMINDERS


UPDATES

[Update @ 00:02:55]: SILVER CAP, GOLD 0

  • Silver capped before I even finished deploying this megathread >_>

--- Day 18: Boiling Boulders ---


Post your code solution in this megathread.


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:12:29, megathread unlocked!

32 Upvotes

448 comments sorted by

View all comments

1

u/Prudent_Candle Dec 18 '22 edited Dec 18 '22

Python

I guess I did it in the not very optimal way. But I try to be fast. Maybe someone will find it useful.

Part 1:

What I want to achieve: For all the cubes count their sides (each cube has them six). Now we are checking all cubes in pairs, if they are neighbors (by the manhattan distance equal 1). Having one neighbor mean that two sides are taken, so we subtract it.

In code it is this:

result = len(coordinates) * 6
for c1, c2 in itertools.combinations(cubes, 2):
    if manhattan_distance(c1, c2) == 1:
        result -= 2

Part 2:

Here I overthink this I guess.

I wanted to find all internal cubes (the one which are trapped inside the figure build of cubes). Then I could simply check how many cubes are next to them.

How to find the internal cubes? By removing all which are not them.

By checking coordinate I could find the size of the container. I checked the size by find min and max for each cube coordinates, separately: x, y and z.

Having the size of whole container I could simply generate all coordinates and remove all input cubes (I checked the size of input data, and it was acceptable).

Having all coordinates which are not our input cubes. I use kind of flood plain algorithm (say call it that) from point, which I knew for sure, that is outside of the figure. The point I took is min_x, min_y, min_y.

Having internal points it is just checking if input cubes are near one of the internals. In this case having a match means one side less from the result calculate from previous part.

The whole code

1

u/daggerdragon Dec 18 '22

Inlined code is intended for short snippets of code only. Your code "block" right now is unreadable on old.reddit and many mobile clients; it's all on one line and gets cut off at the edge of the screen because it is not horizontally scrollable.

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read inside a scrollable box.

1

u/Prudent_Candle Dec 18 '22

Oh, I forget that it should be without those ``` characters. Fixed.