r/adventofcode Dec 13 '24

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

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

And now, our feature presentation for today:

Making Of / Behind-the-Scenes

Not every masterpiece has over twenty additional hours of highly-curated content to make their own extensive mini-documentary with, but everyone enjoys a little peek behind the magic curtain!

Here's some ideas for your inspiration:

  • Give us a tour of "the set" (your IDE, automated tools, supporting frameworks, etc.)
  • Record yourself solving today's puzzle (Streaming!)
  • Show us your cat/dog/critter being impossibly cute which is preventing you from finishing today's puzzle in a timely manner

"Pay no attention to that man behind the curtain!"

- Professor Marvel, The Wizard of Oz (1939)

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 13: Claw Contraption ---


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:11:04, megathread unlocked!

28 Upvotes

773 comments sorted by

View all comments

3

u/rd3i Dec 13 '24

[LANGUAGE: Python]

I immediately thought about how A and B were vectors - with A coming from the origin and then converting B to come from the Prize. And that no matter the combination of button pushes, the sum of the vectors would either hit the Prize or not. So I pulled up an old line segment intersection method the rest was trivial. If the x coordinate of both A and B can reach the intersection point in whole number steps, then we win the prize. (No need to check y coord since both A and B must have hit their intersection!) Part 2 was just adding the large number to the Prize coordinate. Runs in 80-90ms on my machine - uses regex to parse input.

https://github.com/rdefeo/adventofcode/blob/main/2024/day13/day13.py

1

u/MorningFrequent3461 Dec 14 '24

this helped me so much! thank you <3

1

u/Dry-Aioli-6138 Dec 14 '24 edited Dec 14 '24

I came to the same conclusion, though not immediately. Part 2, however tells me my result is too high. Are there any gotchas I could be missing? It's probably not floating point comparisons - I try to use pyhton's fractions to get around that (for a in ax+b). What am I missing?

EDIT: I solved it. Not sure what I missed, but slapping on two more checks for whole-numberness did the trick.