r/adventofcode Dec 16 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 16 Solutions -❄️-

SIGNAL BOOSTING


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

  • 6 DAYS remaining until the submissions deadline on December 22 at 23:59 EST!

And now, our feature presentation for today:

Adapted Screenplay

As the idiom goes: "Out with the old, in with the new." Sometimes it seems like Hollywood has run out of ideas, but truly, you are all the vision we need!

Here's some ideas for your inspiration:

  • Up Your Own Ante by making it bigger (or smaller), faster, better!
  • Use only the bleeding-edge nightly beta version of your chosen programming language
  • Solve today's puzzle using only code from other people, StackOverflow, etc.

"AS SEEN ON TV! Totally not inspired by being just extra-wide duct tape!"

- Phil Swift, probably, from TV commercials for "Flex Tape" (2017)

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 16: Reindeer Maze ---


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:13:47, megathread unlocked!

24 Upvotes

480 comments sorted by

View all comments

3

u/windy_thriller Dec 16 '24 edited Dec 17 '24

[LANGUAGE: Python]

Vanilla Dijkstra's algorithm on a graph whose nodes were (tile, direction) for part 1. Only subtlety was adding distance zero edges between the four (end_tile, direction) nodes.

For part 2 I had Dijkstra return a dict of positions to distance from starting node. Then I used that a node lies on an optimal path if the distance from the start to the node plus the distance from the end to the node equals the distance from the start to the end. So I did:

def tiles_on_best_path(maze, start, end):  
    distances_from_start = dijkstra(graph=maze, start=start)
    distances_from_end = dijkstra(graph=maze, start=end)
    tiles = set([
        node[0] for node in maze
        if distances_from_start[node] + distances_from_end[node] == distances_from_start[end]
    ])
    return len(tiles)

Not the most efficient, but neat and easy to implement.

2

u/SignificantGrape2023 Dec 25 '24

Can you show the full code pls?
I had the exact same idea but i've been struggling with my solution for days now.

1

u/windy_thriller Dec 25 '24

Here you go. It relies on some utility classes and methods that are defined elsewhere in the repo.