In a sense, AoC is officially over as today it's the day after Christmas. I finally have some time free from other obligations and time-bound events, so hopefully I can get a few more of these out. Maybe even finish before I go back to work just after the new year!
It looks like it's simply a problem of finding the symmetry. I remember a few years ago there was a similar symmetry problem, consisiting of folding a piece of paper (on the same day no less!). For part one it doesn't seem that bad. The algorithm for testing reflection is simple (compare row/column); and when found, keep track of how many rows/columns are not part of the reflection.
bool isSymmetricRow(vector<string> field, int row)
{
for (int i = 0; i < field.size(); i++)
{
int lower = row + i + 1;
int upper = row - i;
if (upper < 0 || lower >= field.size()) break;
if (field[lower] != field[upper]) return false;
}
return true;
}
bool isSymmetricColumn(vector<string> field, int col)
{
for (int i = 0; i < field[0].size(); i++)
{
int lower = col + i + 1;
int upper = col - i;
if (upper < 0 || lower >= field[0].size()) break;
for (auto row : field)
{
if (row[lower] != row[upper]) return false;
}
}
return true;
}
Note to self - don't pre-optimize. I lost about 30 minutes because I tried to calculate a "limit" before those for-loops instead of adding the bounds checking in the loop, and I kept getting bugs.
But that works for Part 1!
Oh, a "smudge" is on all of the mirrors, and the reflection is in a different spot... great.
Actually, I suppose this won't be too much more difficult - if I change the functions to instead return a number of reflective differences instead of just breaking, I can look for the one reflection difference of 1. And all I have to do is replace the return false
with a count++
!
int symmetricRowDiff(vector<string> field, int row)
{
int count = 0;
for (int i = 0; i < field.size(); i++)
{
int lower = row + i + 1;
int upper = row - i;
if (upper < 0 || lower >= field.size()) break;
for (int j = 0; j < field[i].size(); j++)
{
if (field[lower][j] != field[upper][j]) count++;
}
}
return count;
}
int symmetricColumnDiff(vector<string> field, int col)
{
int count = 0;
for (int i = 0; i < field[0].size(); i++)
{
int lower = col + i + 1;
int upper = col - i;
if (upper < 0 || lower >= field[0].size()) break;
for (auto row : field)
{
if (row[lower] != row[upper]) count++;
}
}
return count;
}