A room is 30 feet long, 12 wide and 12 high; a spider sits on one end wall, halfway across and a foot below the ceiling, and a fly sleeps on the far end wall, halfway across and a foot above the floor. Walking only on the walls, floor and ceiling, how far does the spider have to go to reach the fly?
Put the room in coordinates: along it from to , across from to , up from to . The spider is at (0, 6, 11) and the fly at (30, 6, 1). A walk is any path on the inside surface of the box.
Two facts cut the problem down to size before any arithmetic.
A shortest walk is straight on every face. A face is flat, and on anything flat the shortest way between two points is a straight line. So a shortest walk is a chain of straight pieces that bend only where it crosses from one face to the next.
It visits each face at most once, and never goes through a corner of the room. If it entered a face twice, the straight segment from where it first entered to where it last left lies in that face, because a rectangle is convex, and is shorter. At a corner, three right angles of paper meet, 270° instead of the 360° around a point on a flat sheet. A walk through such a point has less than 180° of paper on one side, and can be shortened by cutting across that side.
So a shortest walk runs through a sequence of distinct faces, from the spider's wall to the fly's, crossing each shared edge once. There are only 28 such sequences, and each has a best walk.
This is the film's argument, done for every sequence rather than one. Take a sequence of faces and lay it flat, swinging each face down about the edge it shares with the one before. Lengths on each face are unchanged, so a walk through those faces becomes a path in the plane of the same length, and the shortest path in the plane is the straight line from spider to fly.
There is one catch. The straight line is a walk on the room only if it crosses each crease inside the crease and in order; if it leaves the paper, it passes through floor or wall that is not there. Of the 28 nets, 16 keep the line on the paper. The shortest of them is the film's:
where along the sheet and across it. Every other walk is the straight line on one of the other nets, and all of those are longer.
This one never unfolds anything. Fix a sequence of faces. The walk is then decided by where it crosses each shared edge: call the crossing points , each sliding along its edge, with the spider and the fly. The length is
Each term is the distance between two points that move along straight lines, which is a convex function of where they are, and a sum of convex functions is convex. So has exactly one minimum, like a string pulled tight through rings on the edges. At that minimum, moving a little along its edge gains nothing, which says
where the two angles are the ones the walk makes with the edge as it arrives and as it leaves: it crosses every edge at equal angles. That is exactly what a straight line does when the two faces are laid flat. So the tight string is the straight line on the net, found without the net. When the line would leave the paper, the minimum is instead at the end of an edge: the string snags on a corner of the room, and the walk is longer.
The script pulls the string tight on all 28 sequences from 6 random starts each. Every start lands on the same length, the 16 on-the-paper sequences give exactly their straight lines, the other 12 snag on a corner and are at least 48.803, and the best of all is 40, reached only by the two mirror routes.
The film tries four routes before it cuts the room open. Laid flat, each is a straight line, and the two legs of the right triangle it is the long side of are all there is to it:
The obvious walk is 42 and its net agrees: both insects are halfway across, so there is no corner to cut. Going round a side wall swaps a short climb for a long one and is worse, 43.174. Cutting the corner between the end wall and the ceiling is the honest near miss, 40.719, and it loses to the five-face route by 0.72 of a foot. The real contest is 40.72 against 40, not 42 against 40.
The winning walk, face by face, is a straight line of slope folded four times, so every piece of it is a 3-4-5 triangle:
| Face | Leaves at | Length |
|---|---|---|
| near wall | (0, 5.25, 12) | 1.25 |
| ceiling | (7, 0, 12) | 8.75 |
| side wall | (23, 0, 0) | 20 |
| floor | (30, 5.25, 0) | 8.75 |
| far wall | the fly | 1.25 |
Here are all 28 sequences. The mirror image over the other side wall, and the half-turn that swaps the two end walls and the floor with the ceiling, pair them up into these kinds. "Pulled tight" is proof two's length for each:
| Through | Nets | Straight line | Pulled tight |
|---|---|---|---|
| ceiling, side, floor | 2 | 40.000 | 40.000 |
| ceiling, side | 4 | 40.719 | 40.719 |
| floor | 2 | 42.000 | 42.000 |
| side | 2 | 43.174 | 43.174 |
| floor, side | 4 | 47.518 | 47.518 |
| floor, side, ceiling | 2 | 57.271 | 57.271 |
| ceiling, side, floor, side | 4 | 48.270, off the paper | 49.222 |
| side, floor, side | 4 | 48.374, off the paper | 48.803 |
| floor, side, ceiling, side | 4 | 62.370, off the paper | 62.740 |
The nets whose line leaves the paper are not walks at all; pulled tight, each snags on a corner and is longer than its straight line.
Change the room and move the insects. The shortest walk is the first route in the list; choose any other to see its net.
| Through | Nets | Straight line | Pulled tight |
|---|---|---|---|
| ceiling, side, floor | 2 | 40.000 | 40.000 |
| ceiling, side | 4 | 40.719 | 40.719 |
| floor | 2 | 42.000 | 42.000 |
| side | 2 | 43.174 | 43.174 |
| floor, side | 4 | 47.518 | 47.518 |
| floor, side, ceiling | 2 | 57.271 | 57.271 |
| ceiling, side, floor, side | 4 | 48.270, off the paper | 49.222 |
| side, floor, side | 4 | 48.374, off the paper | 48.803 |
| floor, side, ceiling, side | 4 | 62.370, off the paper | 62.740 |
Keep the width, the height and the insects, and change the length . The three contenders are the obvious walk, ; the best four-face walk, ; and the five-face walk, .
The five-face route adds the most across, against against , and the least along, against against . In a short room the extra width costs more than the length it saves; in a long one, the other way round. The obvious walk wins up to 19.4 feet, four faces win from there to 24.2, and five faces win in every longer room:
| Length | Obvious walk | Shortest | Faces |
|---|---|---|---|
| 12 | 24 | 24.000 | 3 |
| 16 | 28 | 28.000 | 3 |
| 19.4 | 31.4 | 31.400 | 3 or 4 |
| 22 | 34 | 33.615 | 4 |
| 24.2 | 36.2 | 35.531 | 4 or 5 |
| 30 | 42 | 40.000 | 5 |
| 40 | 52 | 48.374 | 5 |
| 60 | 72 | 66.483 | 5 |
The film's thirty feet is comfortably in the five-face range. The saving keeps growing as the room lengthens, 5.52 feet in a room of 60, but it creeps up towards ten feet and never gets there: along the sheet the five-face route is only ten feet shorter than the obvious walk, and the 24 across always costs something.
Put the spider feet below the ceiling and the fly feet above the floor. The obvious walk is whatever is, but the cleverer routes get worse as grows, because the spider and the fly end up farther apart along the sheet:
| p | Shortest | Faces |
|---|---|---|
| 0.5 | 39.205 | 5 |
| 1 | 40.000 | 5 |
| 1.5 | 40.804 | 5 |
| 2 | 41.231 | 4 |
| 3 | 41.785 | 4 |
| 4 | 42.000 | 3 |
| 6 | 42.000 | 3 |
Five faces win only while is below 1.65; four faces win up to 3.37; above that, the obvious walk is the shortest there is. One foot from the ceiling and the floor is what makes the five-face answer.
Move the spider a foot in from a top corner of its wall, and the fly a foot in from a bottom corner of the far wall:
| Corners | Through | Flat | Shortest |
|---|---|---|---|
| Diagonally opposite | ceiling, side | 32 × 22 | 38.833 |
| On the same side | side | 32 × 10 | 33.526 |
Diagonally opposite, a four-face walk wins, 38.833, which is what the episode's notes say. On the same side, the answer is a three-face walk round the side wall, 33.526. Either way, the five-face reveal is gone.
Of everywhere the spider could walk to, is the fly the farthest? No. Search a one-foot grid over all six faces, 1,950 points, then finely on the far wall, and the farthest point is on the far wall, halfway across, 1.592 feet above the floor, at 40.475 feet: 0.475 of a foot farther than the fly. It is where the five-face route and the four-face route that cuts the ceiling corner become equally long, and with their mirror images four walks reach it at once. Moving it a little in any direction shortens one of them.
Everything above used only two things: the faces are flat, and the box is convex. So the method works on any convex solid with flat faces. List the sequences of faces, lay each flat, keep the straight lines that stay on the paper, and take the shortest. It fails on a curved surface: a sphere cannot be flattened without stretching, so there is no net to draw a line on, and shortest paths have to be found another way.
The film's route and the brute force agree. scripts/spider.mjs reads the film's own room.json, unfolds all 28 sequences, and finds the same winner. It is the same five faces in the same order, and it crosses each edge at the point the film paints. It finds the same tie with the mirror route and the same lengths for all 16 on-the-paper nets.
It then checks the answer a third way, one that assumes neither of the two facts above. It puts points every so often along all twelve edges of the room and joins every pair that share a face with a straight piece. Then it searches that graph from spider to fly with Dijkstra's algorithm. Walks that revisit a face or pass through a corner are allowed. Every walk in it is a real walk, so it can never beat the true answer, and it gets closer as the points get denser:
| Points every | Points | Links | Shortest walk |
|---|---|---|---|
| 3 ft | 70 | 1,784 | 40.511 |
| 1.5 ft | 142 | 7,216 | 40.239 |
| 1 ft | 214 | 16,296 | 40.033 |
| 0.5 ft | 430 | 65,424 | 40.033 |
| 0.25 ft | 862 | 262,176 | 40.000 |
At a quarter of a foot it finds exactly 40, because the best walk crosses the edges at whole quarters of a foot.
| Check | Cases | Result |
|---|---|---|
| Every face sequence, unfolded | 28 | 16 on the paper; best 40, twice |
| Every face sequence, pulled tight | 168 starts | best 40, the same two |
| Whole surface, no unfolding | 5 graphs | nothing below 40 |
| Room lengths 12 to 80 | 681 | winner switches at 19.4 and 24.2 |
| The lab's random rooms | 600 | same walk as the page's code |
cm. Unfold two faces that meet along an edge into a 1 by 2 rectangle; the two corners are at opposite ends of its diagonal. Along a face diagonal and then an edge is , and along three edges is 3.
19.4 feet. The best four-face walk is , and it ties with when , that is . The five-face walk takes over at 24.2, where .
43 feet, with a walk of 51: the five-face sheet is by , and , which is times the triangle 15, 8, 17. The next is 68, with 74.
Around a corner there are only 270° of paper, three right angles. A walk through the corner leaves less than 180° of paper on one side of it, and on that side the two pieces of the walk near the corner can be joined by a straight cut that misses the corner and is shorter. In the script, every sequence whose string snags on a corner is longer than the best, and the graph search, which allows corners, finds nothing below 40.