AoC 2024: Day 17

Main thread for Day 17 of the 2024 advent of code. Feel free to discuss the challenge and ask questions. If particular discussions become large, we can break them off into new threads.

Some Rules:

  1. Please try to keep spoilers within spoiler tags. This is done by typing [spoiler]text to be blurred[\spoiler]. If you have longer sections you can also use the [details=“Title”] tags.
  2. Have Fun

Day 17 Challenge

Templates:

Resources:

Previous days discussions

Day 1: AoC 2024: Day 1
Day 2: AoC 2024: Day 2
Day 3: AoC 2024: Day 3
Day 4: AoC 2024: Day 4
Day 5: AoC 2024: Day 5
Day 6: AoC 2024: Day 6
Day 7: AoC 2024: Day 7
Day 8: AoC 2024: Day 8
Day 9: AoC 2024: Day 9
Day 10: AoC 2024: Day 10
Day 11: AoC 2024: Day 11
Day 12: AoC 2024: Day 12
Day 13: AoC 2024: Day 13
Day 14: AoC 2023: Day 14
Day 15: AoC 2024: Day 15
Day 16: AoC 2024: Day 16

[LANGUAGE: Zig]

Some reading comprehension issues, again, but then it’s just a simple implement-the-spec task in part1. Part2 relies on observing that the code iterates through 1 3-bit piece of the input at a time, and reverse-engineers the full code by iteratively simulating candidates.

Source: aoc2024/src/day17.zig at main · p88h/aoc2024 · GitHub
Video: https://youtu.be/HEOAl6aGn-M

        parse   part1   part2   total
day 17: 55.0 ns  0.3 µs  9.8 µs 10.2 µs (+-0%) iter=9110
3 Likes

Finally got around to this. I seem to have taken a very similar approach to @p88h; not sure, I’m super happy with part 2 because it forces you to make several assumptions about the input that (at least to me) are not clear from the description. For details on the assumptions, see the comments on the solve function.

https://zigbin.io/a25ede

1 Like

Day 17 was a lot harder than the previous entries and since it took more work, I figured I might as well share my solution for part 2… I adapted parsing code from @erikwastaken for this cleaned up version as my original didn’t have any parsing (just copy&pasted the input into code).

I wrote a lot of experiment code trying to work out any patterns in how the output changes with register A. Eventually figured out how to output the last byte of the input program, that A is processed 3 bits at a time, and that the last outputs depend only on the higher bits of A. Thus it was possible to reconstruct the output program in reverse. Not sure if my queue based algorithm is “good” in an objective sense but it was pretty fast and got the job done.
I think my solving process would’ve been easier had I written out the input program into code manually to analyze it better instead of guessing from outputs.

https://zigbin.io/a5f703

(I don’t know how to add the AoC spoiler guard into zigbin pastes!)