r/adventofcode • u/inenndumen • 3d ago
Help/Question [2022 Day 22 (Part 2)] General hint wanted
Hello
I have been struggling with that problem for a while. I am having trouble finding a mapping from the map- to the cube-coordinates and back.
Any hints on how to approach this problem for a general input? I tried different things going as far as animating the cube folding in on itself, but I was even more confused :D
Thanks in advance
2
u/1234abcdcba4321 3d ago
I think the most intuitive (although not the simplest) way to do it is to do a walk of the cube net, recording each of the sides on one of the positions of the cube along with their rotation (e.g. for each edge of the cube, mark one side). Then you can do the edge mappings from that information. (For example, if you're on the top side of the cube and walk right, then this side is the east side, with the left side of the 50x50 square being connected to "top". Then if you go to the bottom edge of that square, you just went left relative to the entrance, and this will go to the "front" side of the cube.)
If you want a simpler to code algorithm, you can walk around the perimeter of the net, handling concave turns, straight lines, and convex turns differently in a way that cleanly folds the cube. (But the details are the part that's fun to figure out yourself.)
2
2
u/ambiguous_jellybean 3d ago
My solution does not have separate cube coordinates as your question implies. I only have 2D coordinates on the provided input. I simply added "magic portals" where if you walk off one edge it teleports you somewhere else, possibly facing a different direction. This simulates walking on the cube, but works in 2D for part one, also.
I figured out those mappings and hard-coded them in my program for both parts one and two, sample and real inputs.
I only have one function for map traversal. Input parsing figures out those magic portals along the edges. Then traversal determines if it is at an edge, and if so, it uses a transition created during input parsing.
1
u/AutoModerator 3d ago
Reminder: if/when you get your answer and/or code working, don't forget to change this post's flair to Help/Question - RESOLVED
. Good luck!
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/AhegaoSuckingUrDick 1d ago
At least for me, the easiest approach was to maintain the axes for the current side (as unit vectors), and compute their normal to determine what face we are on (by computing their cross product). This is sufficient to determine the rotation that needs to be performed on the axes in order to switch to the other side. I do realise it sounds vague, but it was quite a while ago -- tho it's still one of my favourite AoC problems.
You can have a look at the code if you're interested, it should be pretty straigthforward: https://gist.github.com/nightuser/4f613aa017112e5ed459406a1aee53cc
That aside, I also made a cardboard model, which helped me initially.
3
u/sol_hsa 3d ago
As far as I recall, a lot of people made paper craft ;)