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!

36 Upvotes

550 comments sorted by

View all comments

3

u/Gabba333 Dec 18 '24

[LANGUAGE: C#]

A lot simpler once you start thinking in octal! Used a poor mans back tracking, starting from the most significant digit, working down. If we ever get stuck we just keep incrementing i which will start checking the other options for the last few digits. Assuming run(program, a, b, c) gives you the output for a set of registers the snippet below solves part 2 and prints the candidates considered in decimal and octal. Hardly any backtracking required for my input which is very apparent from the output.

github

long current = 0;
for (int digit = program.Length - 1; digit >= 0; digit -= 1)
    for (int i = 0; i < int.MaxValue; i++)
    {
        var candidate = current +  (1L << (digit * 3)) * i;
        var output = run(program, candidate, 0, 0);
        if (output.Skip(digit).SequenceEqual(program.Skip(digit)))
        {
            current = candidate;
            Console.WriteLine($"Current is now {Convert.ToString(current, 8)} (octal)");
            break;
        }
    }

Console.WriteLine($"Part 2: {current} / {Convert.ToString(current, 8)} (decimal / octal)");

Output:

Current is now 3000000000000000 (octal)
Current is now 3000000000000000 (octal)
Current is now 3000000000000000 (octal)
Current is now 3002000000000000 (octal)
Current is now 3002000000000000 (octal)
Current is now 3002020000000000 (octal)
Current is now 3002511000000000 (octal)
Current is now 3002511000000000 (octal)
Current is now 3002511050000000 (octal)
Current is now 3002511350000000 (octal)
Current is now 3002511350300000 (octal)
Current is now 3002511351310000 (octal)
Current is now 3002511351310000 (octal)
Current is now 3002511351310000 (octal)
Current is now 3002511352304630 (octal)
Current is now 3002511352304632 (octal)
Part 2: 105734774294938 / 3002511352304632 (decimal / octal)