PuzzleReels
Sections

The Chameleons

The maths · Episode 10 · 2:47 · gives the answer

The puzzle

An island has 13 red chameleons, 15 green and 17 blue. When two of different colours meet, both turn into the third colour, and when two of the same colour meet nothing happens.

Can the island ever be all one colour?

The maths, properly

Write an island as its three counts, red · green · blue, so the film starts at 13 · 15 · 17. A meeting takes one off two of the counts and adds two to the third. The total, 45, never changes.

Proof one: count them in threes

This is the film's proof. A meeting changes the counts by −1, −1 and +2. Counted in threes, taking one away and adding two are the same step, because 2≡−1(mod3). So a meeting moves all three remainders along by the same amount, and the gaps between them never change.

13, 15 and 17 leave remainders 1, 0, 2, all different. The differences r−g, g−b and b−r are −2, −2 and 4, and each of those is 1 in threes. An island of one colour has two counts at zero, so one of its differences is 0. A difference that starts at 1 in threes stays at 1, so that never happens.

Proof two: number the colours

Give each colour a number, red 0, green 1, blue 2, and add up every chameleon's number:

S=0·r+1·g+2·b

When an x and a y meet, both become the third colour z. Since x+y+z=0+1+2=3, the sum changes by

2z−x−y=2z−(3−z)=3(z−1)

which is a multiple of 3. So S, counted in threes, never changes. The film's island has S=15+2·17=49, which leaves 1.

An island that is all red has S=0, all green has S=45 and all blue has S=90. All three are multiples of 3, because 45 is, and 49 is not. Done.

This route sees something the first one hides: the total matters. The three one-colour islands have S=0, N and 2N, where N is the number of chameleons. When N is a multiple of 3 those leave the same remainder, so an island can reach all three or none of them. When it is not, they leave three different remainders, and exactly one is allowed.

Proof three: try every island

The film says that failing is not a proof, and it is right: any number of failed attempts proves nothing. Failing everywhere is a different matter. From 13 · 15 · 17 a search that follows every meeting from every island it finds turns up exactly 360 islands, and none of them is one colour. That settles this island, by checking.

It also shows more. There are 1,081 ways to split 45 chameleons into three colours, and S sorts them into three classes of 361, 360, 360. The 360 islands the film's island can reach are exactly the 360 with S leaving 1. So the invariant does not just block the one-colour islands. It is the whole map: every island it allows, the chameleons can actually reach.

A worked case: the film's island

The film counts the first four meetings of its first attempt in threes. The remainders change every time, but they stay all different, and S stays where it was:

The first four meetings of attempt one, counted in threes
MeetingIslandIn threesS in threes
start13 · 15 · 171, 0, 21
red meets green12 · 14 · 190, 2, 11
red meets blue11 · 16 · 182, 1, 01
red meets blue10 · 18 · 171, 0, 21
red meets green9 · 17 · 190, 2, 11

So "the pattern never changes" means the gaps between the remainders, not the remainders themselves. They turn round like a wheel: 1, 0, 2, then 0, 2, 1, then 2, 1, 0.

The first attempt keeps pairing the reds off, and after 22 meetings it reaches 0 · 44 · 1: one chameleon away from an all-green island. The only meeting left is green with blue, and that makes 2 · 43 · 0. The second attempt stops at 0 · 2 · 43.

That first attempt got as close as anyone can. The most chameleons of one colour the island can ever hold is 44, and only three islands manage it:

The closest the film's island can come to one colour
IslandIn threesReachable from 13 · 15 · 17?
0 · 44 · 10, 2, 1yes, in 16 meetings
1 · 0 · 441, 0, 2yes, in 15 meetings
44 · 1 · 02, 1, 0yes, in 17 meetings
1 · 44 · 01, 2, 0no
0 · 1 · 440, 1, 2no
44 · 0 · 12, 0, 1no

The second attempt could have done one better. Instead of 43 blue and 2 green, it could have reached 1 · 0 · 44, in 15 meetings.

Here is every island of 45 at once. A point's position is its mix of colours: the corners are the one-colour islands, and the closer a point is to a corner, the more of that colour it has. A meeting is a short step towards the corner of the colour that gained two. Every run in the film stays on dots of its own colour.

all redall greenall blue13 · 15 · 1713 · 16 · 16attempt oneattempt twothe control, 16 meetings
Every island of 45 chameleons, 1,081 of them. Navy: the 360 that 13 · 15 · 17 can reach. Gold: the 361 that 13 · 16 · 16 can reach, with all three one-colour corners. Pale: the other 360.

The film's control moves one chameleon: 13 · 16 · 16 leaves remainders 1, 1, 1, all the same, which puts it among the gold dots with all three corners. The film takes it to all green in 16 meetings, and that is the fewest possible: every meeting removes at most one blue chameleon, and there are 16 to remove.

Meet them yourself

Pick an island, or set your own counts, and make the meetings. The lab counts in threes as you go and says which colours are still possible. The "fastest way" buttons play a shortest route, and say "never" when there is none.

Three islands: the fewest meetings to each one-colour island
IslandIn threesAll redAll greenAll blue
13 · 15 · 171, 0, 2nevernevernever
13 · 16 · 161, 1, 1161616
13 · 15 · 181, 0, 018nevernever

When it can be done

The claim has a second half: if the other two counts agree in threes, the island really can end all colour k, in exactly as many meetings as the larger of those two counts.

Call the other two colours a and b. Meet an a with a b until one of them runs out. Each of those meetings takes one off both, so their difference stays the same, and it was a multiple of 3. What is left is 3m chameleons of one colour, say x, and some of colour k. Now three meetings move three of them across:

  • a k meets an x, and both become the third colour, y;
  • a y meets an x, twice.

Count it up: x loses 3, y gains 2 and loses 2, and k loses 1 and gains 4. Repeat m times. The number of meetings is min(a,b)+3m=max(a,b).

No route can be quicker. All a chameleons of one colour and all b of the other have to go, and a meeting removes at most one of each, so it takes at least max(a,b) meetings.

Three islands: the fewest meetings to each one-colour island
IslandIn threesAll redAll greenAll blue
13 · 15 · 171, 0, 2nevernevernever
13 · 16 · 161, 1, 1161616
13 · 15 · 181, 0, 018nevernever

Adding one blue chameleon to the film's island makes 13 · 15 · 18, 46 in all. Now green and blue agree in threes, so the island can end all red, in 18 meetings, and no other colour.

Changing the puzzle

Change the total. With a multiple of 3, an island can end every colour or none, as the film's two islands do. With any other total, a mixed island can always end exactly one colour. The table in "Checking it" counts every case.

Let only one of the pair change. Suppose that when two of different colours meet, just one of them turns into the third colour. Now S changes by z−x, which is not a multiple of 3, and the invariant is gone: from 13 · 15 · 17 the island can reach all 1,078 mixed islands of 45. It still never ends one colour, for a simpler reason. Look at the last meeting. The chameleon that did not change still has its own colour, and the one that changed now has the third, which is a different colour. So that meeting always leaves two colours behind. The trick is gone, but a different argument takes its place.

Taking it further

The invariant is the whole story

The three kinds of meeting add one of (−1,−1,2), (−1,2,−1) or (2,−1,−1) to the counts. Every entry of each is 2 in threes, so any combination of them has all three entries equal in threes, and adds up to zero. Conversely, the difference of the first two is (0,−3,3), so steps of 3 between any two colours are combinations too, and together with one meeting they make every such vector. So the meetings, as arithmetic, can make exactly the changes that keep S. Among all the ways of moving chameleons from one colour to another, that is one change in three, which is why the islands fall into three classes.

Arithmetic is not the whole question, because a meeting needs both colours to be there. Even so, for every mixed island of up to 45 chameleons the script checks that it can reach every island in its class and nothing else. The route above is a proof of the part that matters for the puzzle, the one-colour islands.

Left to chance

What if nobody steers? Suppose that every so often two chameleons of different colours meet, every such pair equally likely. The film's island never finishes, since nothing in its class is one colour. The control island can finish, and in 16 meetings, but chance is not trying to finish. Left to random meetings it takes about 1.3×1013 meetings on average, and it ends red, green or blue a third of the time each, to six decimal places.

The reason is that chance pushes towards balance. A colour with many chameleons turns up in most meetings and loses one every time, and it only gains when the two other colours meet. So random meetings drag the counts towards a third each, and finishing means climbing all the way out against that pull. By the time the island finishes, it has long forgotten where it started, which is why the three colours come out equal. Starting from a third of each, the wait grows by a factor of about eight for every three more chameleons:

Random meetings, starting with a third of each colour
ChameleonsMeetings before one colour, on averageGrowth per three more chameleons
626—
9270×10.4
122,302×8.5
1518,727×8.1
18148,960×8.0
211.2×106×7.8
249.1×106×7.8
277.0×107×7.7
305.3×108×7.7
363.1×1010×7.6
451.3×1013×7.5

Checking it

None of this is left to the proofs. scripts/chameleons.mjs asserts every number on this page, and the page is built from what it writes.

The film. It reads island.json, the file the film plays, and re-derives all of it with the rules in site/lib/chameleons-core.mjs: every meeting in both attempts and in the control is legal, the attempts end where the narration says, the rebound is green meeting blue, and the control's 16 meetings are the fewest possible.

Every meeting. On every island of up to 60 chameleons, all 107,970 possible meetings move the three remainders by the same step and leave S alone.

Every mixed island of up to 45. That is 17,160 islands. For each one the script finds everything it can reach, checks that it is exactly the island's class, checks that it can end colour k exactly when the rule says so, and checks that the fewest meetings found by searching equals the larger of the other two counts. The route described above is played out every one of the 17,100 times it applies, and arrives in that many meetings. With a total that is not a multiple of 3, every mixed island can end exactly one colour:

Every mixed island, by the number of chameleons: how many can end exactly one colour, any colour, or none
TotalMixedOneAnyNone
23300
37016
4121200
5181800
6250718
7333300
8424200
95201636
10636300
11757500
128802860
304930163330
4510780358720

The variant. With only one of the pair changing, no meeting from any of the 39,530 mixed islands of up to 60 chameleons lands on one colour.

Random meetings. The chance of each ending and the expected number of meetings are solved exactly, as a Markov chain on the 358 mixed islands of the control's class. The method is checked against ordinary elimination on small islands, and against 20,000 simulated runs on islands small enough to finish.

The lab. The lab runs the functions in site/lib/chameleons-core.mjs, the same text the script imports. On 4,000 random islands the pasted copy gives the same answers as the module.

Where this shows up

  • The fifteen puzzle. Sliding tiles in a 4 by 4 frame cannot swap just the 14 and the 15. The proof is an invariant of the same kind: the parity of the tiles' order, taken together with the row the gap is in, which no slide can change.
  • Rubik's cube. Twist a single corner of a solved cube in place and it can never be solved by turning faces. The corner twists, counted in threes, always add up to zero.
  • Casting out nines. A sum checked by the remainders of its digit sums is the same move: a quantity that cannot change, compared at both ends, can prove an answer wrong but never right.

Try it yourself

1. Add one blue chameleon to the film's island, making 13 · 15 · 18. Can it become one colour? Which, and how fast?

All red, in 18 meetings, and no other colour. Green and blue leave 0 and 0 in threes, so they agree, and the larger of them is 18. Red and blue leave 1 and 0, red and green 1 and 0, so all green and all blue are impossible. With 46 chameleons, not a multiple of 3, exactly one colour was always going to be possible.

2. The second attempt stops at 0 · 2 · 43. Can the island ever have 44 blue chameleons?

Yes, at 1 · 0 · 44, which the island reaches in 15 meetings. But not at 0 · 1 · 44: its remainders are 0, 1, 2, and the island's are always 1, 0, 2 moved along, so 1, 0, 2 or 0, 2, 1 or 2, 1, 0. In terms of S, 1 · 0 · 44 has S=88, which leaves 1 like the start, and 0 · 1 · 44 has S=89, which leaves 2.

3. Show that the control island, 13 · 16 · 16, cannot turn all green in fewer than 16 meetings.

All 16 blue chameleons have to go, and a meeting turns at most one blue chameleon into something else (two blues meeting changes nothing). So it takes at least 16 meetings. The film's run takes exactly 16.

4. An island has 2 red, 2 green and 2 blue chameleons, and they meet at random. How many colour-changing meetings does it take, on average, before the island is one colour?
  1. The island can finish, since its remainders all agree, but it can wander for a long time first: 2 · 2 · 2 can become 1 · 1 · 4, then 0 · 3 · 3, then 2 · 2 · 2 again. The script solves it exactly as a Markov chain and checks it by simulation, and the table in "Left to chance" shows how fast the wait grows with more chameleons.