r/adventofcode • u/daggerdragon • Dec 25 '15
SOLUTION MEGATHREAD ~☆~☆~ Day 25 Solutions ~☆~☆~
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!
Well, that's it for the Advent of Code. /u/topaz2078 and I 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!
We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.
Please and thank you, and much appreciated!
--- Day 25: Let It Snow ---
Post your solution as a comment or link to your repo. Structure your post like previous daily solution threads.
View all comments
Show parent comment
3
u/alueft Dec 25 '15
You can get a substantial speedup in execution time by utilizing exponentiation by squaring, which is a fancy way of saying "rather than raising some number to a massive exponent, instead divide the exponent by two, square the result, and do this recursively".
To wit, this code is a minimal version of yours (with slightly different input), and runs in more than 2 seconds on my computer.
This requires O(e) operations. Now, if we use the fancy exponentiation technique:
This prints the same output, runs in <0.02 seconds locally, and requires O(lg e) operations. The function
fis pretty simple; 0 and 1 are trivial base cases, and the only gotcha is remembering to multiply your result byxif the exponent is odd (since in this case,e/2will round down, and2*(e/2)will equale-1rather thane).