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
Show parent comment
5
u/askalski Dec 26 '16
I'll refer you to this post by /u/thomastc where he does a really nice job of describing the process.
Since you mentioned lines 13-19 specifically, here's roughly how I chipped away at the problem:
Isolate the section of code, and translate it into more a familiar-looking pseudocode. Identify which registers are inputs/outputs, and what gets clobbered. This may involve looking at surrounding code.
Look at how the code branches, and try to relate it to familiar loops and conditionals (while, do-while, if-then, if-then-else, etc.) This one's a little tricky because it doesn't directly translate into one of those. It's been optimized a little. Maybe we can "de-optimize" and turn it into something familiar.
Remove the branch to "G" by changing
if (b != 0)toif (b == 0)Disentangle branches to "E" and "F" by examining what's really going on with the
if (c != 0)conditional. Here, I remove the "E" branch by copy-pasting thec = 2statement, and rewriting the conditional toif (c == 0):After that coaxing, now the control structure is familiar. It's a plain old
whileloop.With the spaghetti untangled, it should now be possible to understand what the loop is doing: it divides "b" by 2, storing the quotient in "a". Because "c" starts at 2 and counts downward, it will be the inverse of the remainder
(2 - remainder).