This one doesn't seem like it will be too difficult. Maybe I can basically copy something similar to a pathfinding problem. Create a board, find a way to calculate a set of next moves with the cost of those moves, and use a priority queue to move through the permutations. In fact, the rules are so restrictive, it may be easier to hard-code a lot of the structure of everything.
~
Okay, a couple hours. A lot of thinking and trying a few things that didn't get anywhere, but I got it in a good way now. With some hard-coded values, and a good setup, it's basically just going through every possible state, queuing up every valid next state in hash map to prioritize lower energy, and keep going until a final state is found.
struct State {
int energy;
vector<string> rooms;
string hallway;
vector<int> moves;
};
An example of finding states in one of the cases
// A: Enter into desired room from room
if (!shouldLeaveRoom(s, orderMap[c])
&& canMoveHall(s, doorway[i], doorway[orderMap[c]], -1)) {
State next = s;
next.rooms[i].erase(0,1);
int e = roomDepth - next.rooms[i].size();
e += roomDepth - next.rooms[orderMap[c]].size();
e += abs(doorway[i] - doorway[orderMap[c]]);
next.rooms[orderMap[c]] = c + next.rooms[orderMap[c]];
next.energy += e * costs[c];
next.moves.push_back(e);
q.push_back(next);
push_heap(q.begin(), q.end(), lazy());
}
I have a few of these, if you want to look at my code. It takes a bit to run, but not too long. Part 2 will likely be a bit longer though.
Okay. Part 2 is just the same, but more. Maybe my solution will work on the new doubled input?
Yep. After removing one hardcoding, it worked the first time. Amazing! First time both parts literally used the same code for me!