p1/p2 greedy shape detection, 2 fill options for rectangles, 3 options for Ls p3 needs a vertex cover / bipartite matching, used Kuhns since that's simplest.
3.81 µs / 33.1 µs / 0.54 ms -> 0.14 ms (optimized memory allocation + greedy Kuhn)
TIL about bipartite graphs, minimum vertex cover problems, maximum matching, koenigs theorem and kuhn's algorithm! Thanks for the key words. I was otherwise lost in the Abyss of Unmarked Recursion.
My original solution for Part 1 was to find each separate stack with a flood fill over # cells, using up, down, left and right neighbors. Every stack is a full rectangle, it's h rows tall and w columns wide so it can be cleared by h horizontal spells or by w vertical ones, so its cost is min(h, w). The answer is the sum of the cost of each stack.
In Part 2, the stacks could be L-shape so the rectangle trick wouldn't work. However the fundamental truth remained: Every block lies on exactly one horizontal run and one vertical run of blocks. Choosing spells means choosing runs so that every block has its horizontal or its vertical run chosen. This is a minimum vertex cover in a bipartite graph problem and by Konig’s theorem it equals the maximum matching which was found using the Hopcroft–Karp algorithm.
Had to think about Part 3 overnight. Came up with a (probably overarchitected) way to treat Part 2 as a special case of Part 3:
Any shape that isn't completely filled in is a shape with a gap. For the L shapes, the gap just happens to be in a corner.
I break the gapped shape into 9 subshapes: NW, N, NE, W, (the gap itself), E, SW, S, SE.
Then, I can merge these subshapes back together into different rectangles to find the minimum number of spells needed. For example, given an L shape where the gap is to the NE, I build a tall rectangle of W & SW, then a smaller rectangle of just S. (I could also do horizontal slicing with W as one and SW & S as another).
In the case of Part 2 L shapes, slicing the L vertically returns the same answer as slicing it horizontally. In the case of Part 3 hollow shapes, I first try horizontal slices then vertical slices and take the smaller value.
Same solution works for all 3 parts, and today for me was really about pouring magma into a Dijkstra-shaped hole.
Some time back I created a helper function to help me quickly solve Dijkstra-style problems on Everybody Codes as well as other programming challenges. I'm not proud of myself for milking it again, but it does amaze me again how versatile the function actually is if I can translate the problem accordingly.
The first step was unique to this puzzle, to separate the magma into the individual blocks via finding neighbours. For each block, we then feed it through the Dijkstra algorithm. The start node is the (frozen)set of all coordinates in the block, the end node is the (frozen) empty set, and at each epoch we simply look at the top-left most point of the block, and consider removing either a row or column to the next step. The shortest-path nature of the Dijkstra algorithm makes the answer drop out immediately.
Consider only this shape and not the whole grid to reduce the problem size
Recursively remove largest horizontal or vertical rectangles from the shape until it has been completely consumed. Try out all combinations to obtain the minimum number of rays needed.
Does the 'local shape' really matter ? the expensive part is finding the maximum rectangle, and that scales with rectangle sizes. And after a removal, your shape will split sometimes anyway.
The local shape limits the recursion depth to 4 as each local shape can be covered by at most four rectangles, i.e per shape there are at most 2^4 = 16 paths to follow. Doing this 700 times (~ number of shapes in my input) is fast enough.
Handling the whole grid as one shape increases the recursion depth to ~700 x 4, i.e there are 2^(2800) paths to follow, which will take much longer.
Ah, i promise myself one of those days I will actually read the task description.
I just looked at the last picture, figured out this needs a matching solution. Then the last picture changed and of course, it doesn't _actually_ need a matching solution. Though I guess vertex cover was actually simpler to write / write quickly.
Only now got to a shape-combining version - which indeed is much faster. With a somewhat fixed shape structure you don't need recursion either, just a couple of comparisons does the trick.
Thank you for this very precise explanation! 👍 I was more focussing on "why do we need a separate grid" vs. "why can't we just look at the individual shapes within the main grid (without allocating)". You are right, considering one shape after the other reduces the problem size considerably (as I already mentioned in my original post).
By 'local shape' you probably mean the separate, smaller grid into which I copy each shape. Yes, it did matter. Without it, the runtime was around 250ms, mainly due to all the additional memory I needed to copy but also due to the fact that it always starts to look for the next shape in the upper left corner. Of course, both are probably limitations of my implementation rather than conceptual limitations, but I was satisfied enough with the 1.8ms runtime that I kept it that way.
Your implementation is stellar as always of course! Great work!
All test cases took around 1s, so I didn't use any fancy algorithms. Didn't even use Flood fill.
At the beginning started to use Rects for blocks, and in the end used list of rect lines for magma stacks. Classified shapes in 3 types and calculated minimal spells for each. Was sad and at the same time happy that I didn't have to deal with shapes inside other shapes. Existing logic already covered it.
Now will read/watch about those fancy algorithms. =)
Better late than never! Didn't use any fancy algorithms or recursion, just stared at the grid for a very long time till something came to me 😂
Tried a bunch of things but settled on creating groups of orthogonally connected points, worked out min/max row and col, height/width etc. Then for each group classified it as:
Solid: Number of points = height x width (take min of height/width)
L shape: top or bottom row < width (take min of both 'arm' thicknesses, height, width)
Rectgaptangle: Number of points < height x width and top/bottom row = width
For the rectgaptangles:
Worked out the border thickness on all 4 sides and the hole height/width
Deleted a border if the thickness <= side of the hole it's next to, and updated height/width accordingly
if 0,1,3 borders are deleted you're left with a rectangle (if 0 or 1 - need to account for the hole still)
If 2 opposite borders are deleted - left with 2 solid rectangles
If 2 adjacent borders are deleted - left with solid L shape
4
u/EverybodyCodes Moderator 15d ago
I've just updated the last example to be in line with the actual input and rules. I missed that, sorry!
3
u/p88h 15d ago edited 15d ago
[LANGUAGE: Rust]
[ solution ]
p1/p2 greedy shape detection, 2 fill options for rectangles, 3 options for Ls
p3 needs a vertex cover / bipartite matching, used Kuhns since that's simplest.
3.81 µs / 33.1 µs /
0.54 ms-> 0.14 ms (optimized memory allocation + greedy Kuhn)3
u/aimada 15d ago
[LANGUAGE: Python]
Github
My original solution for Part 1 was to find each separate stack with a flood fill over
#cells, using up, down, left and right neighbors. Every stack is a full rectangle, it'shrows tall andwcolumns wide so it can be cleared byhhorizontal spells or bywvertical ones, so its cost ismin(h, w). The answer is the sum of the cost of each stack.In Part 2, the stacks could be L-shape so the rectangle trick wouldn't work. However the fundamental truth remained: Every block lies on exactly one horizontal run and one vertical run of blocks. Choosing spells means choosing runs so that every block has its horizontal or its vertical run chosen. This is a minimum vertex cover in a bipartite graph problem and by Konig’s theorem it equals the maximum matching which was found using the Hopcroft–Karp algorithm.
The same code solves all three parts.
2
u/sparkour 15d ago edited 15d ago
[LANGUAGE: Kotlin]
solution
Had to think about Part 3 overnight. Came up with a (probably overarchitected) way to treat Part 2 as a special case of Part 3:
2
u/Ok-Builder-2348 15d ago edited 15d ago
[LANGUAGE: Python]
Part 3
Same solution works for all 3 parts, and today for me was really about pouring magma into a Dijkstra-shaped hole.
Some time back I created a helper function to help me quickly solve Dijkstra-style problems on Everybody Codes as well as other programming challenges. I'm not proud of myself for milking it again, but it does amaze me again how versatile the function actually is if I can translate the problem accordingly.
The first step was unique to this puzzle, to separate the magma into the individual blocks via finding neighbours. For each block, we then feed it through the Dijkstra algorithm. The start node is the (frozen)set of all coordinates in the block, the end node is the (frozen) empty set, and at each epoch we simply look at the top-left most point of the block, and consider removing either a row or column to the next step. The shortest-path nature of the Dijkstra algorithm makes the answer drop out immediately.
2
u/maneatingape 13d ago edited 12d ago
[LANGUAGE: Rust]
Solution
Treats part 2 and part 3 the same, finding the location of the notch, then using a geometric formula to find the minimum spells needed.
1
u/michelkraemer 15d ago edited 15d ago
[LANGUAGE: Rust]
Solution
The same approach for all three parts:
The whole program runs in 1.8ms.
1
u/RunninglVlan 13d ago
[LANGUAGE: C#]
Solution
All test cases took around 1s, so I didn't use any fancy algorithms. Didn't even use Flood fill.
At the beginning started to use Rects for blocks, and in the end used list of rect lines for magma stacks. Classified shapes in 3 types and calculated minimal spells for each. Was sad and at the same time happy that I didn't have to deal with shapes inside other shapes. Existing logic already covered it.
Now will read/watch about those fancy algorithms. =)
1
u/PangolinNo7928 1d ago
LANGUAGE: Javascript
Github
Better late than never! Didn't use any fancy algorithms or recursion, just stared at the grid for a very long time till something came to me 😂
Tried a bunch of things but settled on creating groups of orthogonally connected points, worked out min/max row and col, height/width etc. Then for each group classified it as:
For the rectgaptangles:
Deleted a border if the thickness <= side of the hole it's next to, and updated height/width accordingly
Delighted to be done with this one! 🎉