r/adventofcode • u/daggerdragon • Dec 19 '16
SOLUTION MEGATHREAD --- 2016 Day 19 Solutions ---
--- Day 19: An Elephant Named Joseph ---
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".
/⧹w+/ IS MANDATORY [?]
[Update @ 00:15] 2 gold, silver cap. Thank you for subscribing to Easter Bunny Facts!
- Fact: The Easter Bunny will sometimes leave eggs in the microchip assembly room.
[Update @ 00:30] 11 gold, silver cap.
- Fact: The Easter Bunny killed everyone who found out how he got these stars.
[Update @ 00:45] 45 gold, silver cap.
- Fact: In space, The Easter Bunny can hear you scream.
[Update @ 01:00] 66 gold, silver cap.
- Fact: The Easter Bunny purposefully deleted your comments.
[Update @ 01:15] 92 gold, silver cap.
- Fact: The Easter Bunny has bottled your frustration. Don't ask why.
[Update @ 01:20] Leaderboard capped!
- Fact: What you have just done is no better than what the Easter Bunny has done. Thief.
Thank you for subscribing to Easter Bunny Facts!
View all comments
1
u/p_tseng Dec 19 '16 edited Dec 19 '16
This one hurt. I wrote a silly O(N2 ) solution to get on the leaderboard but spent the rest of the time trying to figure out how to do better. I eventually decided it was easiest to discover the winner for N by figuring out who the winner is for N - 1 and shifting indices around. This is pretty much what https://en.wikipedia.org/wiki/Josephus_problem says to do. There is a little similarity between parts 1 and 2, but not that much. I figure I have some explaining to do on part 2 though...
We'll work with zero-indexed elves (it makes modular arithmetic easier).
n = 1 is easy: 0 wins.
n = 2 is easy: 0 wins.
How do we go from n = 2 to n = 3?
Well, the winner of n = 2 was 0. Let's call this elf
0@2, and the other elf1@2.Who acts first in n = 3, in other words, who becomes elf
0@3? Let's place a new elf before0@2in turn order. This doesn't work; that elf would eliminate0@2. So, then, it must be1@2who eliminates the new elf sitting to the left of0@2. So then we see that elf1@2becomes0@3. So to translate an@2elf name to an@3elf name, we have to shift all the indices down by 1:1@2becomes0@3,0@2becomes2@3, and a dead elf is1@3. Since0@2was the winner for n = 2, we now know the winner of n = 3:2@3.How do we go from n = 3 to n = 4? Again, place a new elf before
0@3in turn order. This elf would eliminate1@3. But hark, what's this? Now elf0@3would eliminate2@3, because1@3is gone! Argh! It turns out we need to maintain the distance between the new 0@n and the old winner, after the first elimination has happened (bringing the number of elves back to n - 1). This makes sense, because we need the winner to still keep the same turn position when there are n - 1 elves. So we fix this by still placing that elf before0@3, but now we let the elf before this new elf take the first turn. In this case, it's2@3, which eliminates0@3. Now the new elf takes its turn, eliminating1@3, and2@3wins. So we see that2@3becomes0@4.2@3was the winner of n=3, so the winner of n=4 is 0.These next few cases should go by fast.
0@4in turn order, it would eliminate1@4. This is fine because0@4will go next and win. So winner0@4becomes1@5.0@5in turn order, it would eliminate2@5. This is fine,0@5moves, then1@5moves and wins. So winner1@5becomes2@6.0@6in turn order, it would eliminate...2@6. No good, let5@6go first and eliminate1@6. The new elf moves,0@6moves,1@6is skipped,2@6moves and wins. Since2@6was still the third to move when there were six elves, this still works. So,5@6becomes0@7, and winner2@6becomes4@7.At this point, I had seen enough to figure out why the "maintain the distance" rule makes sense.
So then, now we know:
0@(N-1)in turn order.Does this new elf eliminate an elf between itself and the winner in turn order?
0@N,0@(N-1)gets to be1@N, etc.(N-2)@(N-1)) be0@N. The0@(N-1)would be2@N,etc.So that's why we see patterns that were mentioned by others of +1, +1, +1, +2, +2, +2, etc.
To be fair, this solution is still O(N), so it still takes about half a second to run on my computer. But for this problem I think that before I apply a shortcut based on a power of three, it's best to figure out why the +1/+2 pattern emerges as it does, and I believe this has now been achieved.