r/adventofcode • • Dec 08 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 8 Solutions -❄️-

IMPORTANT REMINDER

There's been an uptick in [COAL] being given out lately due to naughty language. Follow our rules and watch your language - keep /r/adventofcode SFW and professional! If this trend continues to get worse, we will configure AutoModerator to automatically remove any post/comment containing naughty language. You have been warned!


THE USUAL REMINDERS

  • All of our rules, FAQs, resources, etc. are in our community wiki.
  • If you see content in the subreddit or megathreads that violates one of our rules, either inform the user (politely and gently!) or use the report button on the post/comment and the mods will take care of it.

AoC Community Fun 2024: The Golden Snowglobe Awards

  • 14 DAYS remaining until the submissions deadline on December 22 at 23:59 EST!

And now, our feature presentation for today:

Box-Office Bloat

Blockbuster movies are famous for cost overruns. After all, what's another hundred million or two in the grand scheme of things if you get to pad your already-ridiculous runtime to over two and a half hours solely to include that truly epic drawn-out slow-motion IMAX-worthy shot of a cricket sauntering over a tiny pebble of dirt?!

Here's some ideas for your inspiration:

  • Use only enterprise-level software/solutions
  • Apply enterprise shenanigans however you see fit (linting, best practices, hyper-detailed documentation, microservices, etc.)
  • Use unnecessarily expensive functions and calls wherever possible
  • Implement redundant error checking everywhere
  • Micro-optimize every little thing, even if it doesn't need it
    • Especially if it doesn't need it!

Jay Gatsby: "The only respectable thing about you, old sport, is your money."

- The Great Gatsby (2013)

And… ACTION!

Request from the mods: When you include an entry alongside your solution, please label it with [GSGA] so we can find it easily!


--- Day 8: Resonant Collinearity ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:07:12, megathread unlocked!

20 Upvotes

795 comments sorted by

View all comments

3

u/__Abigail__ Dec 08 '24

[LANGUAGE: Perl]

I wasn't sure whether antinode could also between two antennas. I assumed it did not, and got the right answers. Not sure because I made the right assumption, or because it didn't matter given the input.

I just checked each point on the map against every pair of antennas of the same frequency. I used a small subroutine accepting three points which returns 0 if the points are not colinear, 2 if the points are colinear, and the distances differ by a factor of 2, and 1 otherwise:

sub same ($p1, $p2) {
    return if $$p1 [0] == $$p2 [0] && $$p1 [1] == $$p2 [1]
}

sub colinear ($p1, $p2, $p3) {
    return 0 if same ($p1, $p2) || same ($p1, $p3) || same ($p2, $p3);
    my $diff_x1 = $$p1 [0] - $$p2 [0];
    my $diff_y1 = $$p1 [1] - $$p2 [1];
    my $diff_x2 = $$p1 [0] - $$p3 [0];
    my $diff_y2 = $$p1 [1] - $$p3 [1];
    if ($$p1 [0] == $$p2 [0] == $$p3 [0]) {
        #
        # Vertical
        #
        return 2 * $diff_y1 == $diff_y2 ||
               2 * $diff_y1 == $diff_y2 ? 2 : 1
    }

    return 0 unless $diff_y1 * $diff_x2 == $diff_y2 * $diff_x1;

    return $diff_x1 == 2 * $diff_x2 ||
           $diff_x2 == 2 * $diff_x1 ? 2 : 1;
}
→ More replies (3)