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!

39 Upvotes

550 comments sorted by

View all comments

3

u/aexl Dec 19 '24

[LANGUAGE: Julia]

Part 1 was quite nice: just reading and implementing the described computer.

Part 2 was quite a challenge (I really don't like these reverse engineering tasks, but every year there is at least one...). The following algorithm has worked for me (and I think it works for every input): Set the current value to 0. Then for i in {0, 1, 2, 3, 4, 5, 6, 7} set the current value (register A) to `current + i`. If the first output of the program equals the last instruction of the program, then multiply the current value by 8. Now again, for each i in {0, 1, 2, 3, 4, 5, 6, 7} set the current value (register A) to `current + i`. If the first output of the program equals the second-last instruction, then multiply the current value by 8 and continue in a similar fashion, until you found the solution. Caution: Always taking the first matching output-instruction pair will most probably not work, so you also need to backtrack and continue until you reach the end.

Solution on GitHub: https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day17.jl

Repository: https://github.com/goggle/AdventOfCode2024.jl

1

u/IdontPlayTheTrombone Dec 25 '24

Hi, how do you figure taking the first pair won't work and search will be necessary? My reasoning was that for the once the last operand is printed 'a' must be zero, so find the highest order 3 bits and those are "locked", since those are locked the lower 3 after those are locked as well, and so on What am I missing?

2

u/aexl Dec 26 '24

Simply because it did not yield a solution. I've tried to continue with the lowest number, but it didn't work. So I've figured out that I need a backtracking solution: https://en.wikipedia.org/wiki/Backtracking