For me, it's Christmas Eve. I've had some eggnog. I'm ready to finish this out.
It doesn't look too hard, which makes sense for the final puzzle. Don't want to have everyone stressing on Christmas proper. Just simulate a board state and generate the next state based on some simple rules.
After writing this in five minutes, and spending an hour finding subtle bugs in my logic, it works! Even works for both parts!
int h = board.size(), w = board[0].size();
int steps = 0;
while(true) {
steps++;
vector<string> next = board;
// 1: East facing cucumbers
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (next[i][j] == '>' && board[i][(j+1)%w] == '.') {
next[i][j] = '.';
next[i][(j+1)%w] = '>';
j++;
}
}
}
// 2: South facing cucumbers
set<pair<int, int>> moved;
for (int i = 0; i < h; i++) {
string nextLine;
for (int j = 0; j < w; j++) {
if (next[i][j] == 'v' && next[(i+1)%h][j] == '.'
&& moved.find(pair(i, j)) == moved.end()
&& moved.find(pair((i+1)%h, j)) == moved.end()) {
next[i][j] = '.';
next[(i+1)%h][j] = 'v';
moved.insert(pair(i, j));
moved.insert(pair(i+1, j));
}
}
}
if (board == next) {
printf("Cucumbers stopped after [%d] steps!\n", steps);
return 0;
}
board = next;
}
Overall, this was amazing. I felt stressed a few nights towards the end, but honestly, this was great. I re-discovered a passion for programming in general.
Honestly, it's made me realized what's been getting me down the last few years in various Software Developer positions is that while I love the challenges of programming, all the problem solving, writing code... it's everything else that stressed me out. I'm a good programmer, I just suck at all the other parts of the job, haha.
Anyway, this was great, and my first year doing it overall. I might hack away at previous years at some point, I'm not sure yet, but for now... this is great. There's snow, both outside and on my screen. It's magical <3