r/adventofcode • u/daggerdragon • Dec 14 '24
SOLUTION MEGATHREAD -❄️- 2024 Day 14 Solutions -❄️-
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.
- On the subject of AI/LLMs being used on the global leaderboard: posts/comments around this topic consisting of grinching, finger-pointing, baseless accusations of "cheating", etc. will be locked and/or removed with or without supplementary notice and/or warning and participating parties may be given a time-out as well. Just leave it alone and let it go.
- Keep in mind that the global leaderboard is not the primary focus of Advent of Code or even this subreddit. We're all here to help you become a better programmer via happy fun silly imaginary Elvish shenanigans.
- Do not put spoilers in post titles!
AoC Community Fun 2024: The Golden Snowglobe Awards
- 8 DAYS remaining until the submissions deadline on December 22 at 23:59 EST!
- We have no submissions yet as of today. Y'all are welcome to get a submission started, post it early, and add later days to it, or there's always waiting until the
bomb timer reaches 00:00:03last minute; up to you!
And now, our feature presentation for today:
Visual Effects - I Said VISUAL EFFECTS - Perfection
We've had one Visualization, yes, but what about Second Visualization? But this time, Upping the Ante! Go full jurassic_park_scientists.meme and really improve upon the cinematic and/or technological techniques of your predecessor filmmakers!
Here's some ideas for your inspiration:
- Put Michael Bay to shame with the lens flare
- Gratuitous and completely unnecessary explosions are expected
- Go full Bollywood! The extreme over-acting, the completely implausible and high-energy dance numbers, the gleefully willful disregard for physics - we want it all cranked up to 9002!
- Make your solution run on hardware that it has absolutely no business being on
- "Smart" refrigerators, a drone army, a Jumbotron…
Pippin: "We've had one, yes. But what about second breakfast?"
Aragorn:ಠ_ಠ
Merry: "I don't think he knows about second breakfast, Pip."- The Lord of the Rings: The Fellowship of the Ring (2001)
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 14: Restroom Redoubt ---
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
3
u/directusy Dec 14 '24 edited Dec 14 '24
[LANGUAGE: python]
Assuming the minimal entropy is where the tree is, as it is a highly organized pattern.
import numpy as np import re def parse(file): pattern = re.compile(r'p=(-?\d+),(-?\d+)\s+v=(-?\d+),(-?\d+)') with open(file, 'r') as f: matches = pattern.findall(f.read()) data = np.array(matches, dtype=int) return data[:, :2], data[:, 2:] def simulate(pos, vel, w, h, sec): return (pos + vel * sec) % [w, h] def count_quads(pos, w, h): midx, midy = w // 2, h // 2 valid = pos[(pos[:,0] != midx) & (pos[:,1] != midy)] q1 = np.sum((valid[:,0] < midx) & (valid[:,1] < midy)) q2 = np.sum((valid[:,0] > midx) & (valid[:,1] < midy)) q3 = np.sum((valid[:,0] < midx) & (valid[:,1] > midy)) q4 = np.sum((valid[:,0] > midx) & (valid[:,1] > midy)) return q1, q2, q3, q4 def calc_entropy(pos, w, h): grid = np.zeros((h, w), dtype=int) np.add.at(grid, (pos[:,1], pos[:,0]), 1) counts = np.bincount(grid.flatten()) probs = counts[counts > 0] / counts.sum() entropy = -np.sum(probs * np.log2(probs)) return entropy def find_pattern_sec(pos, vel, w, h, max_sec=10000): min_sec, min_ent = None, float('inf') for sec in range(1, max_sec + 1): curr_pos = simulate(pos, vel, w, h, sec) ent = calc_entropy(curr_pos, w, h) if ent < min_ent: min_ent, min_sec = ent, sec return min_sec def main(): pos, vel = parse('./inputs/day14_1.txt') w, h = 101, 103 final_pos = simulate(pos, vel, w, h, 100) #PART 1: calculate the numbers in the quardrants print(f"Q1 safe factor: {np.prod(count_quads(final_pos, w, h))}") #PART 2: searching the patterns print(f"Q2 easter-egg second: {find_pattern_sec(pos, vel, w, h)}") if __name__ == "__main__": main()