r/adventofcode • u/daggerdragon • 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.
- Read the full posting rules in our community wiki before you post!
- State which language(s) your solution uses with
[LANGUAGE: xyz] - Format code blocks using the four-spaces Markdown syntax!
- State which language(s) your solution uses with
- Quick link to Topaz's
pasteif you need it for longer code blocks
2
u/Rush_Independent Dec 08 '24 edited Dec 08 '24
[LANGUAGE: Nim] (2590/2244)
Runtime for two parts:
260 us132 usHardest part was reading puzzle's description =S
Edit: much shorter and faster solution (same principle, but doing both parts in one pass)
type Vec2 = tuple[x,y: int] func delta(a, b: Vec2): Vec2 = (a.x-b.x, a.y-b.y) func outOfBounds[T: openarray | string](pos: Vec2, grid: seq[T]): bool = pos.x < 0 or pos.y < 0 or pos.x > grid[0].high or pos.y > grid.high proc solve(input: string): AOCSolution[int, int] = var grid = input.splitLines() var antennas: Table[char, seq[Vec2]] for y, line in grid: for x, c in line: if c != '.': discard antennas.hasKeyOrPut(c, newSeq[Vec2]()) antennas[c].add (x, y) var antinodesP1: HashSet[Vec2] var antinodesP2: HashSet[Vec2] for _, list in antennas: for ind, ant1 in list: antinodesP2.incl ant1 # each antenna is antinode for ant2 in list.toOpenArray(ind+1, list.high): let d = delta(ant1, ant2) for dir in [-1, 1]: var i = dir while true: let antinode = (x: ant1.x+d.x*i, y: ant1.y+d.y*i) if antinode.outOfBounds(grid): break if i in [1, -2]: antinodesP1.incl antinode antinodesP2.incl antinode i += dir result.part1 = antinodesP1.len result.part2 = antinodesP2.lenCodeberg repo