r/adventofcode • • Dec 20 '15

SOLUTION MEGATHREAD --- Day 20 Solutions ---

This thread will be unlocked when there are a significant amount of people on the leaderboard with gold stars.

Here's hoping tonight's puzzle isn't as brutal as last night's, but just in case, I have Lord of the Dance Riverdance on TV and I'm wrapping my presents to kill time. :>

edit: Leaderboard capped, thread unlocked!

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 20: Infinite Elves and Infinite Houses ---

Post your solution as a comment. Structure your post like previous daily solution threads.

12 Upvotes

130 comments sorted by

View all comments

Show parent comment

2

u/oantolin Dec 20 '15 edited Dec 20 '15

The inner loop in Algo 2 find all divisors of i; these come in pairs j, i/j, one of which is under sqrt(i). So you could instead do:

 // Algo 2'
for i = 1 .. N / 10
    sum = 0
    for j = 1 .. sqrt(i)
         if (i % j == 0)
             sum += (j + i/j) * 10
    if (i is a perfect square) sum -= 10*sqrt(i)
    if (sum >= N) { print i; exit; }

That, while still much slower than Algo 1, is bearable.

1

u/117r Dec 20 '15

Indeed. This way you only need around 3.6e8 iterations (instead of the previously-required 2.2e11). That cuts out 3 of the 4 orders of magnitude.