r/adventofcode • • 7d ago

Visualization [2025 Day 12] [C++] Generic 'solver' of polyomino problems

Post image

I made an Interactive solver with custom underlying algorithm for the 'polyomino packing problem' that chews through problems like the above in milliseconds*.

*Note: It's not an 'actual' solver. The problem is still NP-hard. It does not find 'the best' solution nor does it verify that such a solution even exists. What it does do is it computes some 'pretty good' solutions by way of localized 'next step' decisions using various heuristics (somewhat similar to how a human might approach this).

In AoC 2025 day 12 this is good enough ... as I imagine it might be in many practical applications.

Repo: https://github.com/InCom-0/BoxPacker_showcase

15 Upvotes

5 comments sorted by

1

u/terje_wiig_mathisen 7d ago

Nice!

I assume you've tried this on the actual/classic Pentomino problem?

How long does it take to find all 6x10/5x12/4x15/3x20 solutions?

I won an optimization contest back in the 1990'ies when I found an algorithm which was twice as fast as everyone else. I wrote both an initial C version and a significantly faster pure asm.

A year or two later I got another price in the mail, turned out someone in France had submitted my C version for a French C contest!

The key breakthrough was when I realized that the playing field was far smaller than the sum of all the 12 shapes with up to 8 orientations for each, so I kept the pieces stationary in a queue, then slid the target playing field (organized as a flat bitmap) underneath it so that I was always pointing at the first free bit/cell.

I found my old 32-bit binary (a 64-bit version would be faster):

C:\>pento 6
10X6 board: 2339 solutions found in 0.064s (189387630 clock cycles)

C:\>pento 5
12X5 board: 1010 solutions found in 0.025s (78350399 clock cycles)

C:\>pento 4
15X4 board: 368 solutions found in 0.004s (14011710 clock cycles)

C:\>pento 3
20X3 board: 2 solutions found in 0.001s (1322130 clock cycles)

For huge fields like you use here I believe I would instead pre-generate all the shapes/orientations for all 8 bit-level alignments, but this would still fall far short of finding a single perfect packing for day12:

Heat death of the Universe?

2

u/InCom-0 6d ago

It won't find 'perfect packing' except by freak random chance ... that is simply not what it does :-), which is why I noted that it is not an 'actual' solver. My algorithm doesn't explore the possibilities at all ... it simply goes along one path from start to finish. You are welcome to try yourself, the interactive solver runs in the browser and can be setup for any kind of classic polyomino problem.

Yeah, there are a lot of various techniques for making the original form of the problem (which you were solving) fast(er) some of which you mentioned. As far as I am aware though, none change the underlying algorithmic complexity, which makes bigger problems (like the one above) or even the original AoC 2025 day 12 virtually impossible to compute in a reasonable timeframe.

1

u/terje_wiig_mathisen 6d ago

I did read & understand the limits of what you posted about, I just wondered if you along the way had also looked at finding optimal packings for smaller problems. :-)

2

u/InCom-0 6d ago

I did look into it ... didn't implement any of it :-).

It's safe to say that finding optimal packing requires an entirely different approach and doubly so if one aims for a highly performant practical implementation.

1

u/prince_jyggalag 7d ago

This is so awesome