10
votes
Day 8: Resonant Collinearity
Today's problem description: https://adventofcode.com/2024/day/8
Please post your solutions in your own top-level comment. Here's a template you can copy-paste into your comment to format it nicely, with the code collapsed by default inside an expandable section with syntax highlighting (you can replace python
with any of the "short names" listed in this page of supported languages):
<details>
<summary>Part 1</summary>
```python
Your code here.
```
</details>
Nothing too special today. I'm enjoying having built-in points and rectangles, though I'm still getting tripped up indexing from 1. I made use of
combinations:atATimeDo:
which, along with other combinatorics tools, will likely come back in future days.Pharo Smalltalk Solution
The last two days have been annoyingly challenging for me. I only obtained one star in the last two days :(
So it was nice to get an easy one today.
Part 1 | Part 2
Hopefully I'll have some time tonight and tomorrow to catch up on previous days.
I found this pretty easy compared to the last couple days. I was confused about how the puzzle worked when first reading it, but once I figured that out I had almost no struggles implementing a solution. I should probably split the
Vector2_Int
stuff into a separate file at this point, since it seems to be getting used in almost every puzzle...Solution (Jai)
Another easy day and one that didn't have Rhai's performance hurting me with the solution I came up with.
I was really expecting tonight's to add onto day 3's.
I'm starting to wonder if any Intcode-like thing is coming, I was really looking forward to it.
Rhai Solution
Part 1 (Rust)
Part 2 (Rust)
The only change was to the antinode function.
Straightforward. This is the 4th day in a row that I've used streams to represent infinite sequences, it works well and can turn a lot of problems normally solved with loops into essentially operating on a list, or lists. It lets you separate the logic that generates values from the logic that acts on those values.
Both parts (Elixir)
I went a little over the top abstracting my code so both parts can reuse a single
solve/2
function...My code for today is kinda blegh but honestly I just wanted to get it done. It wasn't too bad when I'd only done part 1, but I just didn't feel like doing much refactoring during part 2. I am enjoying Racket, though.
Racket
TypeScript
My solution today was a little sloppy because it's past my bedtime. I ended up using a single function for both parts, with a boolean
isPart2
flag argument. Naughty! But it works, and I'm tired, so it stays.Spoilers
Lots of nested loops here! Well we're walking though a grid, so that's one for each axis to start. My solution assembles an object with a different property key corresponding to each frequency char in the map. Every time it finds one it hasn't seen before, it begins a second walk (nesting two more loops) from that point forward to identify the locations of all antennas with that frequency.
Once all the locations have been cataloged, it loops through them to find the antinodes for each pair associated with the same frequency. So, outer loop iterates through frequencies; next level walks through each position within that set; the third level advances through all of the subsequent positions following whichever is being considered in the level above. At this level I have two positions to compare so I grab their row/column deltas to figure out the slope of the line connecting them.
For Part 1 I just evaluate the validity of two possible antinode positions:
posA - delta
, andposB + delta
. If either is in bounds I push it to an array to track unique positions. To speed up the uniqueness check I'm actually casting these to strings first. For Part 2 I added another loop with amultiplier
that increases each iteration. I keep generating new possible antinode positions,posA - delta * multiplier
andposB + delta * multiplier
until both are out of bounds. And I have an ugly hack to count the initial positions as antinodes, ifisPart2
is true only.Parts 1 and 2 (TypeScript)
I constructed complex-valued parametric equations for lines through antennae which is probably overthinking things, but it works. Python's
itertools.combinations
was also useful today. My Python solutions:Part 1
Part 2
Python: Step-by-step explanation | full code
This ended up being simpler than expected. For every pair of matching points we find their slope. Then we step that slope once (or until we hit the edge of the grid) for each point. Once again, having 2-tuples that are easy to add and subtract greatly simplifies these grid problems.
aside: I know that Python's
complex
type also makes this easy, but I find that a little harder to reason about, so I stick with my tuples & methods.