PuzzleReels
Sections

The Spider and the Fly

The maths · Episode 13 · 3:14 · gives the answer

The puzzle

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?

The maths, properly

Put the room in coordinates: x along it from 0 to 30, y across from 0 to 12, z up from 0 to 12. 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.

Proof one: unfold every net

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:

322+242=1600=40

where 32=1+30+1 along the sheet and 24=6+12+6 across it. Every other walk is the straight line on one of the other nets, and all of those are longer.

Proof two: pull a string tight

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 X1,…,Xk, each sliding along its edge, with X0 the spider and Xk+1 the fly. The length is

ℓ=∑j=0k|Xj+1−Xj|

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 Xj a little along its edge gains nothing, which says

cosθin=cosθout

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.

A worked case: the film's room

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:

near wallceilingfar wall42
Up, along, down: 42
near wallsidefar wall4210
Round a side wall: √(42² + 10²) = 43.174
near wallceilingsidefar wall3717
Cut the corner: √(37² + 17²) = 40.719
near wallceilingsidefloorfar wall3224
Five faces: √(32² + 24²) = 40.000

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 24/32=3/4 folded four times, so every piece of it is a 3-4-5 triangle:

The walk of 40, face by face
FaceLeaves atLength
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 wallthe fly1.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:

All 28 ways to unfold the room from the spider's wall to the fly's
ThroughNetsStraight linePulled tight
ceiling, side, floor240.00040.000
ceiling, side440.71940.719
floor242.00042.000
side243.17443.174
floor, side447.51847.518
floor, side, ceiling257.27157.271
ceiling, side, floor, side448.270, off the paper49.222
side, floor, side448.374, off the paper48.803
floor, side, ceiling, side462.370, off the paper62.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.

Unfold it yourself

Change the room and move the insects. The shortest walk is the first route in the list; choose any other to see its net.

All 28 ways to unfold the room from the spider's wall to the fly's
ThroughNetsStraight linePulled tight
ceiling, side, floor240.00040.000
ceiling, side440.71940.719
floor242.00042.000
side243.17443.174
floor, side447.51847.518
floor, side, ceiling257.27157.271
ceiling, side, floor, side448.270, off the paper49.222
side, floor, side448.374, off the paper48.803
floor, side, ceiling, side462.370, off the paper62.740

What changes if the puzzle is changed

A longer or shorter room

Keep the width, the height and the insects, and change the length L. The three contenders are the obvious walk, L+12; the best four-face walk, (L+7)2+172; and the five-face walk, (L+2)2+242.

The five-face route adds the most across, 24 against 17 against 0, and the least along, L+2 against L+7 against L+12. 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:

-4-20246 2030405060 room length, feet 19.424.2 the film the obvious walk five faces four faces feet saved
Room 12 by 12, insects a foot from ceiling and floor
LengthObvious walkShortestFaces
122424.0003
162828.0003
19.431.431.4003 or 4
223433.6154
24.236.235.5314 or 5
304240.0005
405248.3745
607266.4835

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.

The insects higher or lower

Put the spider p feet below the ceiling and the fly p feet above the floor. The obvious walk is p+30+(12−p)=42 whatever p is, but the cleverer routes get worse as p grows, because the spider and the fly end up farther apart along the sheet:

Room 30 long; spider p below the ceiling, fly p above the floor
pShortestFaces
0.539.2055
140.0005
1.540.8045
241.2314
341.7854
442.0003
642.0003

Five faces win only while p 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.

The insects in the corners

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:

Insects a foot in from the corners
CornersThroughFlatShortest
Diagonally oppositeceiling, side32 × 2238.833
On the same sideside32 × 1033.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.

Taking it further

The fly is not the farthest point

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.

Any box, any convex solid

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.

Checking it

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:

A graph over the whole surface, searched with no unfolding
Points everyPointsLinksShortest walk
3 ft701,78440.511
1.5 ft1427,21640.239
1 ft21416,29640.033
0.5 ft43065,42440.033
0.25 ft862262,17640.000

At a quarter of a foot it finds exactly 40, because the best walk crosses the edges at whole quarters of a foot.

What was checked
CheckCasesResult
Every face sequence, unfolded2816 on the paper; best 40, twice
Every face sequence, pulled tight168 startsbest 40, the same two
Whole surface, no unfolding5 graphsnothing below 40
Room lengths 12 to 80681winner switches at 19.4 and 24.2
The lab's random rooms600same walk as the page's code

Where this shows up

  • Running cable. A cable clipped along walls and ceilings is a walk on a box, and the shortest run is found by unfolding the walls, exactly like this.
  • Shortest paths on surfaces. Finding shortest paths on a solid made of flat faces is a standard problem in computational geometry, with uses in robotics and terrain maps. The classic algorithms are organised unfoldings: they spread distances out from the start across faces, as if the surface were being laid flat one face at a time.
  • The puzzle itself. Henry Dudeney published it in 1903. It has outlived nearly everything else of its kind, because the answer looks wrong until the room is cut open.

Try it yourself

1. An ant at one corner of a sugar cube with sides of 1 cm walks on the surface to the opposite corner. How far is the shortest walk?

5≈2.236 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 1+2≈2.414, and along three edges is 3.

2. In the film's room, how long must the room be before the obvious walk stops being the shortest?

19.4 feet. The best four-face walk is (L+7)2+172, and it ties with L+12 when (L+12)2=(L+7)2+289, that is 10L+95=289. The five-face walk takes over at 24.2, where (L+2)2+576=(L+7)2+289.

3. Keep the width, height and insects and lengthen the room. After 30 feet, what is the next whole number of feet for the length at which the shortest walk is also a whole number of feet?

43 feet, with a walk of 51: the five-face sheet is 45 by 24, and 452+242=512, which is 3 times the triangle 15, 8, 17. The next is 68, with 74.

4. Why can't a shortest walk pass through a corner of the room?

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.