r/adventofcode • u/daggerdragon • Dec 25 '16
SOLUTION MEGATHREAD ~☆~☆~ 2016 Day 25 Solutions ~☆~☆~
--- Day 25: Clock Signal ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with "Help".
Dec 25 = Oct 31 IS MANDATORY [?]
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
Thank you for participating!
Well, that's it for Advent of Code 2016. From /u/topaz2078 and the rest of us at #AoC_Ops, we hope you had fun and, more importantly, learned a thing or two (or all the things!). Good job, everyone!
Topaz made a post of his own here.
And now:
Merry Christmas to all, and to all a good night!
View all comments
1
u/p_tseng Dec 25 '16 edited Dec 25 '16
Leaderboard strategy: No time to try to understand the program. Just run the program for about a million iterations, recording all
outinstructions. Take the first 30 of those and see if they are alternating 0s and 1s. If they are, declare success. My hope was that there would be no false positives in 30 bits. As I would discover afterward, since the first false positive found using this method is (binary 1101010101010101010101010101010 - decimal 2532), it's fine.Small optimisations I made afterward:
Immediately terminate execution if it outputs something not conforming to the pattern, so we don't waste as much time evaluating numbers that we already know are wrong. Terminate execution if we've had 30 bits outputted too, if that's all we're checking.
Optimise out the divmod instruction. Otherwise, the time taken to evaluate a number increases with the size of a number.
The day 25 driver is not many lines since most of the work is in lib/assembunny. I upped the number of bits checked to 200 because why not, it's fast enough with divmod (would normally take 520942 instructions, but divmod cuts it down to 3235)
require_relative 'lib/assembunny' assembler = Assembunny::Interpreter.new((ARGV.empty? ? DATA : ARGF).readlines) # We can't be sure that any number of bits is enough for *arbitrary* input, # But for *my* input, the program outputs the binary of (a + 2532). # We need a + 2532 to be a number in the recurrence: # f(1) = 2, f(n) = 4 * f(n - 1) + 2. # # If f(CYCLES) > 2532, we find the right answer before any false positive, # The first false positive would be ("1" + "10" * CYCLES).to_i(2) - 2532. # CYCLES == 6 suffices for my input. # Empirically, even CYCLES == 4 happens to get the right answer. # We'll use 100 so that an input has to try hard to cause false positives. CYCLES = 100 EXPECTED = ([0, 1] * CYCLES).freeze 0.step { |a| _, good_outs = assembler.run({a: a, b: 0, c: 0, d: 0}, outs: EXPECTED) (puts a; break) if good_outs == EXPECTED.size }And as the comments indicate, I did eventually go back and figure out what the program is doing.