--- Day 8: Antinodes ---

December 8

Listening to: breakcore


The description for today is very.... confusing. But I don't think the problem itself isn't very complicated, just hard to communicate:

For each pair of matching characters

  • Draw a line l between the pairs
  • Extend l in each direction by the same length as the original length
  • The ends of the new line are where to mark
var antiNodes = new HashSet<(int, int)>();
foreach (var antenna in antennas.Values)
{
    for (int i = 0; i < antenna.Count - 1; i++)
    {
        for (int j = i + 1; j < antenna.Count; j++)
        {
            int diffX = antenna[j].Item1 - antenna[i].Item1;
            int diffY = antenna[j].Item2 - antenna[i].Item2;

            if (Utils.IsInRange2d(antenna[i].Item1 - diffX, antenna[i].Item2 - diffY, height, width))
            {
                antiNodes.Add((antenna[i].Item1 - diffX, antenna[i].Item2 - diffY));
            }

            if (Utils.IsInRange2d(antenna[i].Item1 + diffX + diffX, antenna[i].Item2 + diffY + diffY, height, width))
            {
                antiNodes.Add((antenna[i].Item1 + diffX + diffX, antenna[i].Item2 + diffY + diffY));
            }
        }
    }
}

For part 2, it looks like my solution is pretty good! I just have to extend that to, instead of just looking at one antinode in each direction, to simply keep looking in each direction until it's out of range!

// Test reverse
int testX = antenna[i].Item1;
int testY = antenna[i].Item2;

while (Utils.IsInRange2d(testX, testY, height, width))
{
    antiNodes.Add((testX, testY));
    testX -= diffX;
    testY -= diffY;
}

// Test forward
testX = antenna[i].Item1;
testY = antenna[i].Item2;

while (Utils.IsInRange2d(testX, testY, height, width))
{
    antiNodes.Add((testX, testY));
    testX += diffX;
    testY += diffY;
}

Easy!


Time taken: 30 minutes

My solutions for today's puzzles