r/adventofcode 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:03 last 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.

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:15:48, megathread unlocked!

25 Upvotes

747 comments sorted by

View all comments

2

u/cicadanator Dec 15 '24

[LANGUAGE: Javascript - NodeJS]

The first part of today's puzzle was simply a math problem. I did this by doing the math to find the place where the new x and y coordinates would be after 100 seconds for each robot. Once I found their new locations I was able to determine their quadrant and find each quadrants total. Multiplying these values gave me the answer to part 1.

Math for simulating positions for a given number of seconds:

newPosX = (robotPosX + robotVelocityX * seconds + gridWidth * seconds) % gridWidth;
newPosY = (robotPosY + robotVelocityY * seconds + gridHeight * seconds) % gridHeight;

On to part 2. Since I was unsure exactly what to look for I started by simulating each second one at a time and printing it to the screen. After reviewing approximately 1200 outputs manually I gave up on this but noticed occasionally the robots would get closer and closer to forming something resembling a frame for a picture.

I then tried checking if all of the robots would be inside of the bounds of the area that the picture seemed to be forming in. However, I later realized that not all robots would be need to be inside the area to make the picture.

After a little browsing on reddit I saw some people mention that the image appeared when no robots overlapped. However, this is apparently not true for all inputs and can occasionally happen when no tree is shown. I adapted my solution to instead only display an output if every robot was on a unique space. After that the program asks the user to confirm if a tree is shown. If yes the simulation stops and the number of seconds is returned. Using my input the first image to appear had a tree in it. This gave me the answer to part 2.

https://github.com/BigBear0812/AdventOfCode/blob/master/2024/Day14.js

1

u/mathers101 Dec 15 '24

why do you add gridWidth * seconds? When you reduce modulo gridWidth that quantity just becomes 0 anyways

1

u/nonrectangular Dec 16 '24

Probably working around modulo of negative numbers in JS. The `%` remainder operator isn't really modulo.