DEV Community

Cover image for Advent of Code 2024 - Day8: Resonant Collinearity
Grant Riordan
Grant Riordan

Posted on • Edited on

5

Advent of Code 2024 - Day8: 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

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one server—search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay