In part two the rules have changed, and each elf now takes from the elf across the circle. If there is an even number of elves, take from the elf directly across. Fo example, with 12 elves in a circle (like a clock face), Elf 1 takes from Elf 7. With an odd number of elves, directly across the circle falls between two elves, so choose the one that is earlier in the circle. For example, with 11 elves, Elf 2 takes from the Elf at position 7. Now who ends up with the presents?

This is tougher. I can't think of a simple range expression to describe who gets eliminated in a round. But I can represent the circle as a list and write a loop to eliminate elves one at a time. Again, if I did that with a del statement for each elf, it would be O(N2). But if instead I do one round at a time, replacing each eliminated elf with None in the list, and then filtering out the None values, then each round is only O(N), and sine there will be log(N) rounds, the whole thing is only O(N log(N)). That should be reasonably fast.

It is still tricky to know which elf to eliminate. If there are N elves, then the elf at position i should elminate the one at position i + N // 2. But we have to skip over the already-eliminated spaces; we can do that by keeping track of the number of eliminated elves in the variable eliminated . We also need to keep track of the current value of N , since it will change. And, since I don't want to deal with the headaches of wrapping around the circletconn, I will only deal with the first third of the elves: the first third all eliminate elves in the other two-thirds; if we went more than 1/3 of the way through, we would have to worry about wrapping around. (I had a bug here: at first I just iterated through N // 3 . But when N is 2, that does no iteration at all, which is wrong; with two elves, the first should eliminate the other. It turns out it is safe to iterate through ceil(N /3) on each round.)