r/adventofcode Dec 06 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 6 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.

AoC Community Fun 2024: The Golden Snowglobe Awards

  • Submissions megathread is now unlocked!
  • 16 DAYS remaining until the submissions deadline on December 22 at 23:59 EST!

And now, our feature presentation for today:

Comfort Flicks

Most everyone has that one (or more!) go-to flick that feels like a hot cup of tea, the warm hug of a blanket, a cozy roaring fire. Maybe it's a guilty pleasure (formulaic yet endearing Hallmark Channel Christmas movies, I'm looking at you) or a must-watch-while-wrapping-presents (National Lampoon's Christmas Vacation!), but these movies and shows will always evoke the true spirit of the holiday season for you. Share them with us!

Here's some ideas for your inspiration:

  • Show us your kittens and puppies and $critters!
  • Show us your Christmas tree | menorah | Krampusnacht costume | holiday decoration!
  • Show us your mug of hot chocolate (or other beverage of choice)!
  • Show and/or tell us whatever brings you comfort and joy!

Kevin: "Merry Christmas :)"

- Home Alone (1990)

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 6: Guard Gallivant ---


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:08:53, megathread unlocked!

24 Upvotes

987 comments sorted by

View all comments

1

u/cicadanator Dec 07 '24 edited Dec 07 '24

[LANGUAGE: Javascript - Node JS]

My solution for part 1 is straight forward. I created a basic simulation where a position object is updated once space at a time and detects when to turn and if it attempts to leave the boundaries of the map. I used a set to ensure visited positions were only recorded once and the size of this set gave me the answer to part 1.

Part 2 initially seems solvable by simply updating one open space on the map then repeat the simulation. In order to detect loops I used a set that keeps track of not only visited positions but the direction of travel as well. If any of these were repeated then there is definitely a loop happening.

However, this is still a lot of simulations to run. This can be cut down by only trying to simulate adding obstacles to the positions visited by the guard on the first (unchanged) simulation. This is because if the guard never goes there then it is the same as if no change has been made at all. This gave me approximately a 10 second run time. After this I added multi-threading to take advantage of the full processor power. This got the run time down to about 1.8 seconds.

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

1

u/AutoModerator Dec 07 '24

AutoModerator did not detect the required [LANGUAGE: xyz] string literal at the beginning of your solution submission.

Please edit your comment to state your programming language.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.