After how long I got stuck for on yesterday's puzzle, I'm really hoping today's is a bit easier. It would be nice to get a bit more sleep with this being done.
This seems like an odd pixel-processing thing... actually, it seems more like a convoluted cellular automaton with a very specific ruleset. This might be kinda fun actually!
One potential hitch is that with an infinite grid, it may be a bit tricky to expand everything.
void rebuffer(vector<string> &image, char c) {
for (int i = 0; i < image.size(); i++) {
image[i].insert(image[i].begin(), c);
image[i].insert(image[i].end(), c);
}
string top, bottom;
top.insert(0, image[0].size(), c);
bottom.insert(0, image[0].size(), c);
image.insert(image.begin(), top);
image.insert(image.end(), bottom);
}
int getIndex(vector<string> image, int x, int y) {
int index = 0;
if (x == 0 || y == 0 || x == image.size()-1 || y == image[0].size()-1) return 0;
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
index = (index <<= 1) + (image[x+i][y+j] == '.' ? 0 : 1);
return index;
}
vector<string> image;
for (int iter = 0; iter < ITERATIONS; iter++) {
vector<string> newImage;
for (int x = 0; x < image.size(); x++) {
string line;
for (int y = 0; y < image[0].size(); y++)
line += ref[getIndex(image, x, y)];
newImage.push_back(line);
}
rebuffer(newImage, newImage[0][0]);
image = newImage;
}
That works pretty well. Just a quick count of the final resulting number of #
in the image, and we're good! A little tweaking to handle the ever-expanding border and it works out. It even grows on its own just fine, which is great, considering that part 2 is likely going to be "do this for 100 iterations" or something.
~
I did forget to handle the 'edge' case for the infinite possabilities. Luckily, with enough of a buffer (at least 2, though I used 5), all you need to do is assume an index of either 0 or 511. For the input it looks like it fluctuates from on to off for the entire field. Tricky! Either way, I have an answer, and on to part 2!
Yep. Same question, just more iterations. Easy!
Also, haha, blob