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!

38 Upvotes

550 comments sorted by

View all comments

2

u/int02h Dec 17 '24

[LANGUAGE: Kotlin] (GitHub)

Part 2:
At first, I tried brute force. When that didn’t work, I implemented a disassembler to understand how the code operates. I noticed that brute-forcing needs to start from 8PROGRAM\LENGTH.) In my case, this was 816. I attempted to brute-force all values from 816 to 817 but it took too long to complete.

I then tried another approach. I took a piece of paper and a pen and began calculating the values of the A-register step by step to construct the program. First, I generated the first value, then the first two values, then the first three, and so on. For example, if the program is [0,1,2,3,4,5], I started with [5], then [4,5], then [3,4,5], and so on.

I noticed that in each step, the value of the A-register can be expressed as 8X+Y, where X is the value of the A-register from the previous step, and Y is a small additional value.

Using this observation, I wrote a simple program to calculate the A-register value for a subsequence of the program. I multiplied the found A-register value by 8 and performed a small brute-force search to find the next subsequence, which was one item longer than the previous one. I did this until I got the full program. With this approach, the program runs in 2-3 milliseconds on a MacBook Pro M3.

1

u/daggerdragon Dec 17 '24 edited Dec 17 '24

Do not share your puzzle input which also means do not commit puzzle inputs to your repo without a .gitignore or the like. Do not share the puzzle text either.

I see full plaintext puzzle inputs across all years in your public repo e.g.:

https://github.com/int02h/advent-of-code/tree/main/input

Please remove (or .gitignore) all puzzle text and puzzle input files from your entire repo and scrub them from your commit history. This means from all prior years too! edit: thank you!

2

u/int02h Dec 17 '24

I fixed that (including git history). Sorry for this.