Deus ex machina is a plot device in which a seemingly unsolvable problem is resolved by a sudden and unexpected external event. For example: "The villain has our hero backed in a corner with no way out. He's finished for sure! Just then, a car swerves out of nowhere and hits the villain."
I'm wondering, what is the opposite of this plot device?
As in, all the pieces which are needed to resolve the problem have been lying in plain view from the beginning of the story, but it is only at the climax that the protagonist puts the pieces together in an unexpected way to solve the problem. If done skillfully, it surprises and delights the audience.
As you can see, I don't mean "diabolus ex machina" (where the unexpected event is misfortunate rather than fortunate), but I want to know what it is called where the ending doesn't rely on unheralded external forces and only involves a (perhaps very clever) assembly of elements already present in the story.
The American TV series "MacGyver" is a corny example of this. MacGyver always saves the day by putting together the ordinary objects he finds around him.
Sadly, "MacGyver" was rather hokey and contrived. He always happened to get locked in a room containing steel wire, metal tubes, a radio, a pair of pliers, a chemistry set, and a canister of black powder. There are other stories that employ this plot device much more dexterously and subtly (but I can't think of them).
What am I describing?