Problem. An island has red, green and blue chameleons. When two of different colours meet, both change to the third colour. Can all become blue?
Answer: yes iff and at least two colours are present (or already).
bool allBecome(long long a, long long b, long long c) { // a, b: counts of the other two colours, c: target colour
int nonzero = (a > 0) + (b > 0) + (c > 0);
if (a == 0 && b == 0) return true;
return (a - b) % 3 == 0 && nonzero >= 2;
}Proof: the mod-3 invariant and a construction
Invariant. A meeting changes the three counts by in some order, so every pairwise difference changes by or : the differences mod 3 are invariant. All blue means , so is necessary; and if only one colour is present, no meeting can happen. Construction. Assume , . While , a red-green meeting decreases both by one; this ends at with . If , a red-blue meeting gives , then two red-green meetings give ; repeat times. A blue chameleon is needed for that step; it exists because at least two colours were present (if initially, then , which creates blue ones). Checked against BFS for all counts up to 7.