Advent of Code Day 24: Computing with Sets
reitzen.com
reitzen.com
I don't really understand how the "early out" logic works but that would have been pretty nice. My solution didn't have that and took quite a bit of time.
The "correct" solution to the problem is, as often is the case for Advent of Code, too tedious to implement: since the only way the program behavior changes is if the states of the registers diverge after an input operation, you can consider inputs that lead to the same states as "related". The coarsest partition with respect to this relation may be found by partition refinement. (https://en.wikipedia.org/wiki/Partition_refinement)
Assuming an efficient representation of the search space, this results in a solution that does the minimum amount of work possible.
As this is only going through the input code once, this is virtually instantaneous and ended up as the day that was fastest of all days to compute.
https://github.com/yxhuvud/aoc21/blob/main/day24.cr
I guess the solution could be classified as meta-analytical.
The early out interpreter I describe should work in general, although it will probably fail to accelerate many programs.
I saw a lot of reverse engineering of the programs in the AoC subreddit, which was part of the reason I decided to write this up. I didn't even read the supplied program when building this solution.
- either you unconditionally append a new digit to Z
- or you conditionally either:
- replace the last digit in Z with a new one
- or you remove the last digit in Z
The only way you would end up with a 0 at the end is if every one of those conditions evaluates such that you always remove the last digit of Z. Each of those 7 conditions constrains 2 of the input digits at a time, and once you list out those conditions, you're done.
Because it doesn't capture the whole state it is vulnerable to false positives. But that in turn probably makes it more memory efficient.
My solution would make Google disappointed in a whiteboard interview, imo.
Skipped it due to time and ability constraints. Going for the cucumbers later...
I built that one after AoC concluded out of curiosity, but the contents of this post were my in the moment solution.
Then I started to think about pruning the search space with early out. If you squint, there's a similarity here to bloom filters. It gives you two possible answers: definitely no, or possibly yes.
I was expecting to run in to issues where the state grew too large, but that turned out never to be the case.
An alternate way to approach it would have been with dynamic programming, but I didn't feel like that would be as much fun.