DEV Community

Cover image for AoC '24 Day 8: Resonant Collinearity
Grant Riordan
Grant Riordan

Posted on • Edited on

AoC '24 Day 8: Resonant Collinearity

Day 8: Resonant Collinearity

This one was a lot tougher than it needed to be because the instructions were so poor (common problem people have found on the forums).

You can find my solution at repo

Concept of solution:

In Part 1, the goal is to find pairs of antennas (points), draw a line between them, and place antinodes 1 step away from each antenna along the line.

In Part 2, for each pair of antennas, the code calculates a directional vector and extends the line in both directions, placing antinodes along the line. The line continues until there are no more valid points (e.g off the grid).

Summary of the "How"

Step 1: Gathering All Points in the Antenna Group
The first step is to collect the positions of all antennas into a list (or similar structure). For example, after reading the input data, you might have something like this:

antennas = 
    'A': [(1, 1), (2, 2)]
    'B': [(3, 3), (4, 4)]
    # More antennas...

Enter fullscreen mode Exit fullscreen mode

Step 2: Generating Pairs of Points
Once you have the positions of all the antennas in the group, the goal is to evaluate every possible pair of points to analyse potential lines between them. The code uses the combinations function to do this.

For each valid point along the extended line that exists in the grid, add it to the HashSet.

Step 3 - Finding the Direction

Once the pairs are generated, they are used to define directional vectors (what tells you how to get from one point to another on a grid, using just two numbers: one for the horizontal direction (left/right) and one for the vertical direction (up/down)) and analyse potential alignments or extensions.

Calculate the Directional Vector:
For a pair (P1, P2) where P1 = (x1, y1) and P2 = (x2, y2), the directional vector (dx, dy) is:

dx = x2 - x1
dy = y2 - y1
Enter fullscreen mode Exit fullscreen mode

Why is a Directional Vector Useful?

Movement:
It helps us understand how to move from one point to another. In the case of a grid, moving right would be a positive horizontal direction, and moving up would be a positive vertical direction.

Line Definition:
If you want to draw a straight line between two points, you can use the directional vector to keep moving from one point to the next along the line. By repeatedly adding the directional vector, you can find every point on the line. Imagine an L-Shaped ruler set at the directional vector and slide it to the next pair.

Imagine you have two points on a grid, like:

Point A: (1, 1)
Point B: (4, 5)
To move from Point A to Point B, you need to know:

  • How much to move horizontally (left or right).
  • How much to move vertically (up or down).

The directional vector tells you this. It’s a simple calculation of the difference between the two points.

Calculation of the Directional Vector:
Horizontal direction (left/right): dx = x2 - x1
Vertical direction (up/down): dy = y2 - y1

For our points:
Point A is at (1, 1), and Point B is at (4, 5).

The directional vector is:
dx = 4 - 1 = 3 (move 3 steps to the right).
dy = 5 - 1 = 4 (move 4 steps up).
So, the directional vector from Point A to Point B is (3, 4).

Scaling:
You can scale the directional vector to make it bigger or smaller by multiplying it by a number. For example, multiplying (3, 4) by 2 gives (6, 8), meaning you're moving twice as far in the same direction.

Extend the Line:
The code then attempts to extend the line defined by this pair, checking both forward and backward along the direction defined by (dx, dy) to find possible aligned points (antinodes).

Again, if you wish to reach out drop me a follow, or find me on twitter

Top comments (0)