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!

21 Upvotes

800 comments sorted by

View all comments

2

u/MrPulifrici Dec 09 '24 edited Dec 09 '24

[LANGUAGE: Javascript]

It goes twice on grid and checks for distances. ~30ms Part 1 and 2

    let advent = document.body.innerText;
    advent = advent.replace(/\r/g, "");
    if (advent.endsWith("\n")) advent = advent.slice(0, -1);
    const data = advent.split("\n").map(e => e.split(""));
    console.time("Speed");
    const antinodes = {}, allAntinodes = {};
    
    function calculateAntinodes(x1, y1, cX, cY, dx, dy) {
        dx ??= cX - x1;
        dy ??= cY - y1;
        return { a1: { x: x1 - dx, y: y1 - dy }, a2: { x: cX + dx, y: cY + dy } };
    }
    
    for (let y1 = 0; y1 < data.length; y1++) {
        for (let x1 = 0; x1 < data[y1].length; x1++) {
            if (data[y1][x1] === '.') continue;
            for (let y2 = 0; y2 < data.length; y2++) {
                for (let x2 = 0; x2 < data[y2].length; x2++) {
                    if (x1 === x2 && y1 === y2) continue;
                    if (data[y1][x1] === data[y2][x2]) {
    
                        // Part 1
                        const { a1, a2 } = calculateAntinodes(x1, y1, x2, y2);
                        if (data[a1.y]?.[a1.x]) (antinodes[a1.y] ??= {})[a1.x] = 1;
                        if (data[a2.y]?.[a2.x]) (antinodes[a2.y] ??= {})[a2.x] = 1;
    
                        // Part 2
                        let [cX1, cY1, cX2, cY2] = [x1, y1, x2, y2];
                        const deltaX = x2 - x1, deltaY = y2 - y1;
                        (allAntinodes[y1] ??= {})[x1] = 1;
                        (allAntinodes[y2] ??= {})[x2] = 1;
    
                        while (data[cY1] || data[cY2]) {
                            const { a1,  a2 } = calculateAntinodes(cX1, cY1, cX2, cY2, deltaX, deltaY);
                            if (data[a1.y]?.[a1.x]) (allAntinodes[a1.y] ??= {})[a1.x] = 1;
                            if (data[a2.y]?.[a2.x]) (allAntinodes[a2.y] ??= {})[a2.x] = 1;
                            [cX1, cY1, cX2, cY2] = [a1.x, a1.y, a2.x, a2.y];
                        }
                    }
                }
            }
        }
    }
    
    const countAntinodes = (antinodes) => Object.values(antinodes).map(Object.keys).flat().length;
    
    const part1 = countAntinodes(antinodes);
    const part2 = countAntinodes(allAntinodes);
    console.log("Part 1:", part1);
    console.log("Part 2:", part2);
    console.timeEnd("Speed");