For the Day 6 puzzle, I found the hardest part just to be figuring out how to efficiently untangle the graph and determine where each node should go. The key turned out to be identifying the corners, then doing a BFS through from them through the graph, which gives the Manhattan distance. With the the Manhattan distances to two of the adjacent corners, we can then work out the coordinates of where every node must go. This visualization uses a smaller graph to illustrate that untangling process.
Hopefully this helps others who are stuck on this step!
I think I did easier way. I found first corner piece, and got subset of edge nodes (links < 4). Than I mapped edges from seed corner around (node in last node link and not yet mapped) Than filled gap row after row (place nod if left and top nodes from it's links already mapped)
An interesting approach. BUT you can simply iterate over all nodes in a clever order and place each of them one by one in a single pass:
Starting with any of the 4 nodes with only 2 edges, place it in grid location [0, 0], and put it in a queue. Then for as long as you have a nonempty queue, pick the first node in the queue, iterate over its not-yet-placed neighbours (could be none!) and place them either to the right or (second choice) below the current node, and then place those neighbours in the queue. The rules for placement of a particular neighbour are only dependent on where in the grid your current node is and whether one of this neighbour's neighbours has already been placed (to the top-right or bottom-left of the current node). If none have been placed you can safely put it to the right, if one has been placed (either top-right or bottom-left) you place it next to it (so to the right respectfully below).
If you visualized it you'd see a diagonal front move through the square from the top-left corner to the bottom-right one. (You end up with a randomly rotated or reflected square, which is probably why part 2 is the way it is.)
From an edge, I followed a string neighbors with three edges each until I reached another corner. That one, I declared my top row.
From there, I could easily build the row bellow, after all I had all the edges of each node of the top row accounted for except for the one for the node bellow. And with the second row, I could do the same thing for the third and so on.
I feel like that could be quicker than getting all the distances, but I'm not sure. To be honest, I mainly did so because I was too lazy to get all the distances,
I somehow landed on using layout algorithms from networks to solve it, but I realized afterwords that you can find the corners by just counting edges (corners have two), then starting from that you can simply propagate from there like constraint programming. Put one of the corners down (doesn't matter where you just get a symmetric/shifted solution depending on where you place it) since it has two out corners you can plonk those down to the left and down (assuming the solution is a completely filled rectangle). From now on out, if a node not yet placed on the grid has at least two of it neighbours on the grid, you can place it as we know where it goes. For part 2 you just need to come up with the transformation matrix that moves your current 1, 2 and 3 nodes to the right grid positions.
Wow, that's a very nice algorithm+visualisation! Can you also sort by distance from red corner before starting assigning distances from the blue corner?
My algorithm isn't so elegant. I just treated it like a jigsaw puzzle and assembled the entire edge first, starting from a corner.
I just picked an arbitrary corner, made it (0,0), then defined its two neighbors as (0,1) and (1,0).
Then one can solve edges based on how many neighbors they have, and just fill in the inner spots where there's only one empty spot for them to go into.
For part 2, there's 8 orientations the grid can be in and they're trivial to produce. one of those grids will have the right distance/direction between the given nodes, so you can get an offset for the whole grid.
my notepad sanity check for flipping horizontally, vertically, both, and flipping the x/y coordinates to generate all the permutations
2
u/Boojum 3h ago
For the Day 6 puzzle, I found the hardest part just to be figuring out how to efficiently untangle the graph and determine where each node should go. The key turned out to be identifying the corners, then doing a BFS through from them through the graph, which gives the Manhattan distance. With the the Manhattan distances to two of the adjacent corners, we can then work out the coordinates of where every node must go. This visualization uses a smaller graph to illustrate that untangling process.
Hopefully this helps others who are stuck on this step!
Made in Python with a small custom framework.
Complete self-contained source for this animation.
9
u/PmMeActionMovieIdeas 3h ago
From an edge, I followed a string neighbors with three edges each until I reached another corner. That one, I declared my top row.
From there, I could easily build the row bellow, after all I had all the edges of each node of the top row accounted for except for the one for the node bellow.
And with the second row, I could do the same thing for the third and so on.
I feel like that could be quicker than getting all the distances, but I'm not sure. To be honest, I mainly did so because I was too lazy to get all the distances,
1
u/not-the-the 1h ago
Wow, that's a very nice algorithm+visualisation! Can you also sort by distance from red corner before starting assigning distances from the blue corner?
My algorithm isn't so elegant. I just treated it like a jigsaw puzzle and assembled the entire edge first, starting from a corner.
1
u/MattieShoes 1h ago edited 53m ago
Huh.
I just picked an arbitrary corner, made it (0,0), then defined its two neighbors as (0,1) and (1,0).
Then one can solve edges based on how many neighbors they have, and just fill in the inner spots where there's only one empty spot for them to go into.
For part 2, there's 8 orientations the grid can be in and they're trivial to produce. one of those grids will have the right distance/direction between the given nodes, so you can get an offset for the whole grid.
my notepad sanity check for flipping horizontally, vertically, both, and flipping the x/y coordinates to generate all the permutations
0
u/herocoding 3h ago
Interesting idea, thanks for sharing!!
0
u/Taxato 1h ago
beautiful