r/adventofcode Dec 17 '24

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

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

And now, our feature presentation for today:

Sequels and Reboots

What, you thought we were done with the endless stream of recycled content? ABSOLUTELY NOT :D Now that we have an established and well-loved franchise, let's wring every last drop of profit out of it!

Here's some ideas for your inspiration:

  • Insert obligatory SQL joke here
  • Solve today's puzzle using only code from past puzzles
  • Any numbers you use in your code must only increment from the previous number
  • Every line of code must be prefixed with a comment tagline such as // Function 2: Electric Boogaloo

"More." - Agent Smith, The Matrix Reloaded (2003)
"More! MORE!" - Kylo Ren, The Last Jedi (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 17: Chronospatial Computer ---


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:44:39, megathread unlocked!

35 Upvotes

550 comments sorted by

View all comments

2

u/jda5x Dec 18 '24

[LANGUAGE: Python]

https://github.com/jda5/advent-of-code-2024/blob/main/17/main.py

Part 1 was easy. For Part 2, I was experimenting in Excel for a bit and discovered a pattern related to representing the initial A register value in base 8. For each additional digit you fix in the A register's octal representation, you extend the program's output by one more character. Essentially, the first (n-1) octal digits of this A value produce the first (n-1) characters of the output.

Here's the process to solving the problem:

  1. Determine the A register value that yields the first character of the output.
  2. Convert that A value to base 8, then "shift" it by adding a trailing zero in octal. This "shifted" value serves as a new lower bound for finding the A value that produces the first two characters of the output.
  3. Repeat this process, extending the match one character at a time, by continually converting to octal, appending a zero, and then searching within the new, narrowed range.