Futurama hasn’t been this good in years. It’s been very funny this season, and I think most of the movies had some inspired gags, but this week’s was the first time the plot’s been as good as the jokes since the good old days. It did what all the best episodes do: found the humour value in an old sci-fi concept and took it to ridiculous extremes.

If you didn’t see it, Farnsworth invented a mind-swapper. He and Amy swapped bodies to enjoy youth and food respectively, but found they couldn’t switch back because their body’s immune response blocked the same switch being made again. They could still swap to other bodies, though, so Bender and the Professor (really Amy) swap minds.

Bender (really the Professor): Now then Amy, we’ll simply switch bodies, and then we’ll… no… I’d be back in my body, but then you and Bender would be switched, and the Amy and Bender bodies can’t trade minds again since they just did!

Professor (really Amy): Oh no! Is it possible to get everyone back to normal using four or more bodies?

Bender (really the Professor): I’m not sure! I’m afraid we need to use… MATH.

You can already tell the whole episode is going to be amazing at this point, but I had to pause and work it out before watching any more. You could call this an intentionally self-inflicted spoiler, but you kind of already know the main characters aren’t going to end up permanently switched, right? I just wanted to know if this was a way they could be restored, and if so how many more people they’d need.

It’s trickier than it seems as first, but not as impossible as it starts to look shortly after that. To be as clear as possible, I’ll refer to people as Person They Appear To Be (Person They Really Are). This is important because it’s the bodies that can’t switch back directly – there’s no rule about minds.

By this point in the show, here’s the story so far:

Amy and the Professor switch Producing:

Professor (Amy)

Amy (Professor) Amy and Bender switch Producing:

Amy (Bender)

Bender (Professor) Leaving:

Professor (Amy) Bender (Professor) proposes switching with Professor (Amy) but doesn’t go through with it. It’s easier to think about if he does do that, though, because we’re back to just two wrong ‘uns to fix. Bender and Professor switch Producing:

Bender (Amy)

Professor (Professor) – Fixed! Leaving:

Amy (Bender) Now Bender and Amy need to switch, but they can’t directly. So we use Fry as temporary storage: Bender and Fry switch Producing:

Fry (Amy)

Bender (Fry) Leaving:

Amy (Bender) But that’s not enough. We need a somewhere else to put Bender’s brain so we don’t end up using the same storage person twice for the same trade. So: Amy and Leela switch Producing:

Amy (Leela)

Leela (Bender) Leaving:

Fry (Amy)

Bender (Fry) Now we can get Amy’s brain back in her without putting Bender into Fry – we can’t re-swap that pair. Amy and Fry switch Producing:

Fry (Leela)

Amy (Amy) – Fixed! Leaving:

Bender (Fry)

Leela (Bender) Similarly, we can put Bender back to rights without stranding Fry. Leela and Bender switch Producing:

Leela (Fry)

Bender (Bender) – Fixed! Leaving:

Fry (Leela) So finally we can switch two people who both want to be switched, which is the only way you can ever finish this thing: Fry and Leela switch Producing:

Fry (Fry) – Fixed!

Leela (Leela) – Fixed!

That was my first attempt. Looking it over, I think there’s probably some flab there – I think I can see a way to save a move or two early on. But figuring out this much made the rest of the episode all the more fun to watch, because the switches get nuts very, very quickly.

It seems to be biting off way more storylines than it can chew, and more maths than it can resolve, but it does both beautifully. The Wash Bucket is one of those sublime minor characters we don’t see enough of lately, like the homeopathy-hating announcer bot in Crimes of the Hot. And although they seem to be glossing over the mess they’ve made by having the Globetrotters announce that any such tangle can be resolved with two extra people, that is provably correct, and they show they’re nerdy enough to do the legwork by doing a montage of all the required switches at the end.

If Futurama sometimes seems weirdly inconsistent, it’s probably because of the crazy number of writers. No two episodes this season have been written by the same person. This one was by Ken Keeler, also behind Time Keeps on Slipping, and I therefore conclude that he is awesome.