# Build a pair of spies that will throw stones into a river

Recently at the newly released Puzzling.SE, there was a problem about spies throwing stones into a river that was actually quite challenging:

Two spies must pass each other two secret numbers (one number per spy), unnoticed by their enemies. They have agreed on a method for doing this using only 26 indistinguishable stones in advance.

They meet at a river, where there is a pile of 26 stones. Starting with the first spy, they take turns throwing a group of stones into the river: the first spy throws some number of stones, then the second one, then the first one again...

Each spy must throw at least one stone on his turn, until all the stones are gone.

They observe all throws and diverge when there are no more stones. They keep silence all the time and no information is exchanged except number of stones thrown at each turn.

How can they exchange the numbers successfully if the numbers can be from 1 to M?

Your task is to build a pair of programs, spy1 and spy2, that can solve this problem for the highest possible M.

Your programs will each take a number from 1 to your chosen M as input. Then, spy1 will output a number representing the number of stones it throws into the river, which will be input to spy2 which will also output a number to be input to spy1, and so on until the numbers output add up to 26. At the end of throwing, each program will output the number it believes the other program had, which must match the number that was actually input to the other program.

Your program must work for all possible ordered pairs of numbers (i, j) where both i and j can vary from 1 to M.

The program that works for the largest M will be the winner, with the first answer to be posted breaking a tie. Additionally, I will award a +100 reputation bounty to the first solution that is proven to work for M >= 2286, and +300 to the first solution that is proven to work for M >= 2535.

• Solution means algorithm, or a program, which generates set of dissisions for each (i,j)? – klm123 Jun 13 '14 at 5:27
• Not one program, but two. They must communicate independently, as in your problem. – Joe Z. Jun 13 '14 at 6:09
• Since the programs are going to need to share their decision tree, can we make it one program which takes an argument to say which spy it is? – Peter Taylor Jun 13 '14 at 9:57
• As long as you can guarantee that each spy communicates independently and there is no extra information exchanged between them. – Joe Z. Jun 13 '14 at 17:35
• Independently I have verified that 2535 is the information-theoretic max for this problem. I quite strongly believe now that no program can do better. – nneonneo Jun 14 '14 at 23:11

## C#, M = 2535

This implements* the system which I described mathematically on the thread which provoked this contest. I claim the 300 rep bonus. The program self-tests if you run it either without command-line arguments or with --test as a command-line argument; for spy 1, run with --spy1, and for spy 2 with --spy2. In each case it takes the number which I should communicate from stdin, and then does the throws via stdin and stdout.

* Actually, I've found an optimisation which makes a massive difference (from several minutes to generate the decision tree, to less than a second); the tree which it generates is fundamentally the same, but I'm still working on a proof of that. If you want a direct implementation of the system which I described elsewhere, see revision 2, although you might want to backport the extra logging from Main and the better inter-thread comms from TestSpyIO.

If you want a test case which completes in less than a minute, change N to 16 and M to 87.

using System;
using System.Collections.Generic;
using System.Linq;

namespace CodeGolf
{
internal class Puzzle625
{
public static void Main(string[] args)
{
const int N = 26;
const int M = 2535;

var root = BuildDecisionTree(N);

if (args.Length == 0 || args[0] == "--test")
{
DateTime startUtc = DateTime.UtcNow;
Console.WriteLine("Built decision tree in {0}", DateTime.UtcNow - startUtc);
startUtc = DateTime.UtcNow;

int ok = 0;
int fail = 0;
for (int i = 1; i <= M; i++)
{
for (int j = 1; j <= M; j++)
{
if (Test(i, j, root)) ok++;
else fail++;
}
double projectedTimeMillis = (DateTime.UtcNow - startUtc).TotalMilliseconds * M / i;
Console.WriteLine("Interim result: ok = {0}, fail = {1}, projected test time {2}", ok, fail, TimeSpan.FromMilliseconds(projectedTimeMillis));
}
Console.WriteLine("All tested: ok = {0}, fail = {1}, in {2}", ok, fail, DateTime.UtcNow - startUtc);
}
else if (args[0] == "--spy1")
{
new Spy(new ConsoleIO(), root, true).Run();
}
else if (args[0] == "--spy2")
{
new Spy(new ConsoleIO(), root, false).Run();
}
else
{
Console.WriteLine("Usage: Puzzle625.exe [--test|--spy1|--spy2]");
}
}

private static bool Test(int i, int j, Node root)
{
TestSpyIO io1 = new TestSpyIO("Spy 1");
TestSpyIO io2 = new TestSpyIO("Spy 2");
io1.Partner = io2;
io2.Partner = io1;

// HACK! Prime the input
io2.Output(i);
io1.Output(j);

Spy spy1 = new Spy(io1, root, true);
Spy spy2 = new Spy(io2, root, false);

th1.Start();
th2.Start();

th1.Join();
th2.Join();

// Check buffer contents. Spy 2 should output spy 1's value, so it's lurking in io1, and vice versa.
return io1.Input() == i && io2.Input() == j;
}

private static Node BuildDecisionTree(int numStones)
{
NodeValue[] trees = new NodeValue[] { NodeValue.Trivial };
for (int k = 2; k <= numStones; k++)
{
int[] prev = trees.Select(nv => nv.Y).ToArray();
List<int> row = new List<int>(prev);
int cap = prev.Length;
for (int i = 1; i <= prev[0]; i++)
{
while (prev[cap - 1] < i) cap--;
}

int[] next = row.OrderByDescending(x => x).ToArray();
NodeValue[] nextTrees = new NodeValue[next.Length];
nextTrees[0] = trees.Last().Reverse();
for (int i = 1; i < next.Length; i++)
{
int cp = next[i] - 1;
nextTrees[i] = trees[cp].Combine(trees[i - prev[cp]]);
}

trees = nextTrees;
}

NodeValue best = trees.MaxElement(v => Math.Min(v.X, v.Y));
return BuildDecisionTree(numStones, best, new Dictionary<Pair<int, NodeValue>, Node>());
}

private static Node BuildDecisionTree(int numStones, NodeValue val, IDictionary<Pair<int, NodeValue>, Node> cache)
{
// Base cases
// NB We might get passed val null with 0 stones, so we hack around that
if (numStones == 0) return new Node(NodeValue.Trivial, new Node[0]);

// Cache
Pair<int, NodeValue> key = new Pair<int, NodeValue>(numStones, val);
Node node;
if (cache.TryGetValue(key, out node)) return node;

// The pair-of-nodes construction is based on a bijection between
//     $\prod_{i<k} T_i \cup \{(\infty, 0)\}$
// and
//     $(T_{k-1} \cup \{(\infty, 0)\}) \times \prod_{i<k-1} T_i \cup \{(\infty, 0)\}$

// val.Left represents the element of $T_{k-1} \cup \{(\infty, 0)\}$ (using null for the $(\infty, 0)$)
// and val.Right represents $\prod_{i<k-1} T_i \cup \{(\infty, 0)\}$ by bijection with $T_{k-1} \cup \{(\infty, 0)\}$.
// so val.Right.Left represents the element of $T_{k-2}$ and so on.
// The element of $T_{k-i}$ corresponds to throwing $i$ stones.
Node[] children = new Node[numStones];
NodeValue current = val;
for (int i = 0; i < numStones && current != null; i++)
{
children[i] = BuildDecisionTree(numStones - (i + 1), current.Left, cache);
current = current.Right;
}
node = new Node(val, children);

// Cache
cache[key] = node;
return node;
}

class Pair<TFirst, TSecond>
{

public Pair(TFirst x, TSecond y)
{
this.X = x;
this.Y = y;
}

public override string ToString()
{
return string.Format("({0}, {1})", X, Y);
}

public override bool Equals(object obj)
{
Pair<TFirst, TSecond> other = obj as Pair<TFirst, TSecond>;
return other != null && object.Equals(other.X, this.X) && object.Equals(other.Y, this.Y);
}

public override int GetHashCode()
{
return X.GetHashCode() + 37 * Y.GetHashCode();
}
}

class NodeValue : Pair<int, int>
{

public static NodeValue Trivial = new NodeValue(1, 1, null, null);

private NodeValue(int x, int y, NodeValue left, NodeValue right) : base(x, y)
{
this.Left = left;
this.Right = right;
}

public NodeValue Reverse()
{
return new NodeValue(Y, X, this, null);
}

public NodeValue Combine(NodeValue other)
{
return new NodeValue(other.X + Y, Math.Min(other.Y, X), this, other);
}
}

class Node
{

public Node this[int n]
{
get { return _Children[n]; }
}

public int RemainingStones
{
get { return _Children.Length; }
}

public Node(NodeValue value, IEnumerable<Node> children)
{
this.Value = value;
this._Children = children.ToArray();
}
}

interface SpyIO
{
int Input();
void Output(int i);
}

// TODO The inter-thread communication here can almost certainly be much better
class TestSpyIO : SpyIO
{
private object _Lock = new object();
private int? _Buffer;
public TestSpyIO Partner;

internal TestSpyIO(string name)
{
this.Name = name;
}

public int Input()
{
lock (_Lock)
{
while (!_Buffer.HasValue) Monitor.Wait(_Lock);

int rv = _Buffer.Value;
_Buffer = null;
Monitor.PulseAll(_Lock);
return rv;
}
}

public void Output(int i)
{
lock (Partner._Lock)
{
while (Partner._Buffer.HasValue) Monitor.Wait(Partner._Lock);
Partner._Buffer = i;
Monitor.PulseAll(Partner._Lock);
}
}
}

class ConsoleIO : SpyIO
{
public int Input()
{
}

public void Output(int i)
{
Console.WriteLine("{0}", i);
}
}

class Spy
{
private Node _Node;
private bool _MyTurn;

internal Spy(SpyIO io, Node root, bool isSpy1)
{
this._IO = io;
this._Node = root;
this._MyTurn = isSpy1;
}

internal void Run()
{
int myValue = _IO.Input() - 1;
int hisValue = 1;

bool myTurn = _MyTurn;
Node n = _Node;
while (n.RemainingStones > 0)
{
if (myTurn)
{
if (myValue >= n.Value.X) throw new Exception("Internal error");
for (int i = 0; i < n.RemainingStones; i++)
{
// n[i] allows me to represent n[i].Y values: 0 to n[i].Y - 1
if (myValue < n[i].Value.Y)
{
_IO.Output(i + 1);
n = n[i];
break;
}
else myValue -= n[i].Value.Y;
}
}
else
{
int thrown = _IO.Input();
for (int i = 0; i < thrown - 1; i++)
{
hisValue += n[i].Value.Y;
}
n = n[thrown - 1];
}

myTurn = !myTurn;
}

_IO.Output(hisValue);
}
}
}

static class LinqExt
{
// I'm not sure why this isn't built into Linq.
public static TElement MaxElement<TElement>(this IEnumerable<TElement> e, Func<TElement, int> f)
{
int bestValue = int.MinValue;
TElement best = default(TElement);
foreach (var elt in e)
{
int value = f(elt);
if (value > bestValue)
{
bestValue = value;
best = elt;
}
}
return best;
}
}
}


### Instructions for Linux users

You'll need mono-csc to compile (on Debian-based systems, it's in the mono-devel package) and mono to run (mono-runtime package). Then the incantations are

mono-csc -out:codegolf31673.exe codegolf31673.cs
mono codegolf31673.exe --test


etc.

• Is that C#? I don't know how to run that on Linux. – Joe Z. Jun 13 '14 at 17:31
• All this time I thought I was doing something wrong. As it turns out, building the decision tree simply takes 30 minutes... For the record, this works on Fedora 20: 1. yum install mono-core (as root). 2. dmcs Puzzle625.cs 3. mono Puzzle625.exe --test – Dennis Jun 14 '14 at 5:24
• @Dennis, I think that Mono's JIT isn't quite as good as Microsoft's. I have some ideas for optimisation, but I haven't finished testing them. – Peter Taylor Jun 14 '14 at 6:48
• Fedora's repositories provide version 2.10.8, which is over two years old. Maybe newer versions are faster. I'm curious: How long does it take with Microsoft? – Dennis Jun 14 '14 at 14:00
• From 30 minutes to 39 microseconds. That's what I call an optimization! – Dennis Jun 16 '14 at 19:21

# Python Tester Program

I figure it would be useful to have a test program which can verify that your implementation is working. Both scripts below work with either Python 2 or Python 3.

Tester program (tester.py):

import sys
import shlex
from subprocess import Popen, PIPE

def writen(p, n):
p.stdin.write(str(n)+'\n')
p.stdin.flush()

MAXSTONES = 26

def test_one(spy1cmd, spy2cmd, n1, n2):
p1 = Popen(spy1cmd, stdout=PIPE, stdin=PIPE, universal_newlines=True)
p2 = Popen(spy2cmd, stdout=PIPE, stdin=PIPE, universal_newlines=True)

nstones = MAXSTONES

writen(p1, n1)
writen(p2, n2)

p1turn = True
while nstones > 0:
if p1turn:
writen(p2, s)
else:
writen(p1, s)
if s <= 0 or s > nstones:
print("Spy %d output an illegal number of stones: %d" % ([2,1][p1turn], s))
return False
p1turn = not p1turn
nstones -= s

if n1guess != n1:
print("Spy 2 output wrong answer: expected %d, got %d" % (n1, n1guess))
return False
elif n2guess != n2:
print("Spy 1 output wrong answer: expected %d, got %d" % (n2, n2guess))
return False

p1.kill()
p2.kill()

return True

def testrand(spy1, spy2, M):
import random
spy1cmd = shlex.split(spy1)
spy2cmd = shlex.split(spy2)

n = 0
while 1:
i = random.randrange(1, M+1)
j = random.randrange(1, M+1)
test_one(spy1cmd, spy2cmd, i, j)
n += 1
if n % 100 == 0:
print("Ran %d tests" % n)

def test(spy1, spy2, M):
spy1cmd = shlex.split(spy1)
spy2cmd = shlex.split(spy2)
for i in range(1, M+1):
print("Testing %d..." % i)
for j in range(1, M+1):
if not test_one(spy1cmd, spy2cmd, i, j):
print("Spies failed the test.")
return
print("Spies passed the test.")

if __name__ == '__main__':
if len(sys.argv) != 4:
print("Usage: %s <M> <spy1> <spy2>: test programs <spy1> and <spy2> with limit M" % sys.argv[0])
exit()

M = int(sys.argv[1])
test(sys.argv[2], sys.argv[3], M)


Protocol: The two spy programs specified on the command-line will be executed. They are expected to interact solely though stdin/stdout. Each program will receive its assigned number as the first line of input. In each turn, spy 1 outputs the number of stones to throw, spy 2 reads a number from stdin (representing spy 1's throw), and then they repeat (with positions reversed). When either spy determines that 26 stones have been thrown, they stop and output their guess for the other spy's number.

Example session with a compatible spy1 (> denotes input to the spy)

> 42
7
> 5
6
> 3
5
27
<program quits>


If you choose a very large M, and it takes too long to run, you can switch test( for testrand( in the last line to run random tests. In the latter case, leave the program running for at least a few thousand trials to build up confidence.

Example program (spy.py), for M=42:

import sys

# Carry out the simple strategy for M=42

def writen(n):
sys.stdout.write(str(n)+"\n")
sys.stdout.flush()

def spy1(n):
m1,m2 = divmod(n-1, 6)
writen(m1+1)

writen(m2+1)

rest = 26 - (m1+m2+o1+o2+2)
if rest > 0:
writen(rest)
writen((o1-1)*6 + (o2-1) + 1)

def spy2(n):
m1,m2 = divmod(n-1, 6)
writen(m1+1)

writen(m2+1)

rest = 26 - (m1+m2+o1+o2+2)
if rest > 0:

writen((o1-1)*6 + (o2-1) + 1)

if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage: %s [spy1|spy2]" % (sys.argv[0]))
exit()

n = int(input())
if sys.argv[1] == 'spy1':
spy1(n)
elif sys.argv[1] == 'spy2':
spy2(n)
else:
raise Exception("Must give spy1 or spy2 as an argument.")


Example usage:

python tester.py 42 'python spy.py spy1' 'python spy.py spy2'


# Java, M = 2535

OK, here is my implementation. At each step one spy makes a move. Each possible move represents a range of codes. The spy chooses the move matching his secret code. As they throw more stones, the range of possible codes reduces until, at the end, for both spies, only one code remains possible according to the moves they did.

To recover the secret codes, you can replay all the moves and compute the corresponding code ranges. At the end, only one code remains for each spy, that is the secret code he wanted to transmit.

Unfortunately, the algorithm relies on a large precomputed table with hundred of thousands of integers. The method couldn't be applied mentally with more than 8-10 stones maybe.

The first file implements the Spy's algorithm. The static part precomputes a codeCount table that is later used to compute each move. The second part implements 2 procedures, one to select how many stones to throw, the other to replay a moves to help reconstruct the secret codes.

The second file tests the Spy class extensively. The method simulate simulates the process. It uses the Spy class to generate a sequence of throws from the secret codes and then reconstructs the codes from the sequence.

Spy.java

package stackexchange;

import java.util.Arrays;

public class Spy
{
// STATIC MEMBERS

/** Size of code range for a number of stones left to the other and the other spy's range */
static int[][] codeCount;

// STATIC METHODS

/** Transpose an array of code counts */
public static int[] transpose(int[] counts){
int[] transposed = new int[counts[1]+1];
int s = 0;
for( int i=counts.length ; i-->0 ; ){
while( s<counts[i] ){
transposed[++s] = i;
}
}
return transposed;
}

/** Add two integer arrays by element.  Assume the first is longer. */
public static int[] add(int[] a, int[] b){
int[] sum = a.clone();
for( int i=0 ; i<b.length ; i++ ){
sum[i] += b[i];
}
return sum;
}

/** Compute the code range for every response */
public static void initCodeCounts(int maxStones){
codeCount = new int[maxStones+1][];
codeCount[0] = new int[] {0,1};
int[] sum = codeCount[0];
for( int stones=1 ; stones<=maxStones ; stones++ ){
codeCount[stones] = transpose(sum);
}
}

/** display the code counts */
public static void dispCodeCounts(int maxStones){
for( int stones=1 ; stones<=maxStones ; stones++ ){
if( stones<=8 ){
System.out.println(stones + ": " + Arrays.toString(codeCount[stones]));
}
}
for( int s=1 ; s<=maxStones ; s++ ){
int[] row = codeCount[s];
int best = 0;
for( int r=1 ; r<row.length ; r++ ){
int min = r<row[r] ? r : row[r];
if( min>=best ){
best = min;
}
}
System.out.println(s + ": " + row.length + " " + best);
}
}

/** Find the maximum symmetrical code count M for a number of stones */
public static int getMaxValue(int stones){
int[] row = codeCount[stones];
int maxValue = 0;
for( int r=1 ; r<row.length ; r++ ){
int min = r<row[r] ? r : row[r];
if( min>=maxValue ){
maxValue = min;
}
}
return maxValue;
}

// MEMBERS

/** low end of range, smallest code still possible */
int min;

/** range size, number of codes still possible */
int range;

/** Create a spy for a certain number of stones */
Spy(int stones){
min = 1;
range = getMaxValue(stones);
}

/** Choose how many stones to throw */
public int throwStones(int stonesLeft, int otherRange, int secret){
for( int move=1 ; ; move++ ){
// see how many codes this move covers
int moveRange = codeCount[stonesLeft-move][otherRange];
if( secret < this.min+moveRange ){
// secret code is in move range
this.range = moveRange;
return move;
}
this.min += moveRange;
this.range -= moveRange;
}
}

/* Replay the state changes for a given move */
public void replayThrow(int stonesLeft, int otherRange, int stonesThrown){
for( int move=1 ; move<stonesThrown ; move++ ){
int moveRange = codeCount[stonesLeft-move][otherRange];
this.min += moveRange;
this.range -= moveRange;
}
this.range = codeCount[stonesLeft-stonesThrown][otherRange];
}
}


ThrowingStones.java

package stackexchange;

public class ThrowingStones
{
public boolean simulation(int stones, int secret0, int secret1){

// ENCODING

Spy spy0 = new Spy(stones);
Spy spy1 = new Spy(stones);

int[] throwSequence = new int[stones+1];
int turn = 0;
int stonesLeft = stones;

while( true ){
// spy 0 throws
if( stonesLeft==0 ) break;
throwSequence[turn] = spy0.throwStones(stonesLeft, spy1.range, secret0);
stonesLeft -= throwSequence[turn++];
// spy 1 throws
if( stonesLeft==0 ) break;
throwSequence[turn] = spy1.throwStones(stonesLeft, spy0.range, secret1);
stonesLeft -= throwSequence[turn++];
}

assert (spy0.min==secret0 && spy0.range==1 );
assert (spy1.min==secret1 && spy1.range==1 );

//      System.out.println(Arrays.toString(throwSequence));

// DECODING

spy0 = new Spy(stones);
spy1 = new Spy(stones);

stonesLeft = stones;
turn = 0;
while( true ){
// spy 0 throws
if( throwSequence[turn]==0 ) break;
spy0.replayThrow(stonesLeft, spy1.range, throwSequence[turn]);
stonesLeft -= throwSequence[turn++];
// spy 1 throws
if( throwSequence[turn]==0 ) break;
spy1.replayThrow(stonesLeft, spy0.range, throwSequence[turn]);
stonesLeft -= throwSequence[turn++];
}
int recovered0 = spy0.min;
int recovered1 = spy1.min;

// check the result
if( recovered0 != secret0 || recovered1 != secret1 ){
System.out.println("error recovering (" + secret0 + "," + secret1 + ")"
+ ", returns (" + recovered0 + "," + recovered1 + ")");
return false;
}
return true;
}

/** verify all possible values */
public void verifyAll(int stones){
int count = 0;
int countOK = 0;
int maxValue = Spy.getMaxValue(stones);
for( int a=1 ; a<=maxValue ; a++ ){
for( int b=1 ; b<=maxValue ; b++ ){
count++;
if( simulation(stones, a, b) ) countOK++;
}
}
System.out.println("verified: " + countOK + "/" + count);
}

public static void main(String[] args) {
ThrowingStones app = new ThrowingStones();
Spy.initCodeCounts(26);
Spy.dispCodeCounts(26);
app.verifyAll(20);
//      app.verifyAll(26); // never managed to complete this one...
}

}


For reference, the precomputed codeCount array contains the following values:

1: [0, 1]
2: [0, 1, 1]
3: [0, 2, 1, 1]
4: [0, 3, 2, 1, 1, 1]
5: [0, 5, 3, 2, 2, 1, 1, 1, 1]
6: [0, 8, 5, 4, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1]


This related directly to Peter Taylor's Tk sets. We have:

(x,y) in Tk  <=>  y <= codeCount[x]

• I don't think this quite meets spec without a way to run the two spies in separate processes and communicate the throws without sharing access to their range fields. But I'm very intrigued by your method of calculating the table. Do you have a proof of correctness? And are you interested in collaborating on a paper which discusses the problem and calculating its solution? – Peter Taylor Oct 6 '14 at 22:20
• The other spy's range is a function of the past moves, as it is computed in the "replay" method. I believe it is correct. The table I compute is exactly the same as you sets Tk. Transposing the table exchanges x and y, the sum is the sum of all possible children from a node. I haven't proven it correct, except that I have tested it up to 22 stones. I tried to write a proper answer for puzzling.stackexchange, but I haven't managed to explain it in a clear an convincing way. And mostly, it is what you did already. – Florian F Oct 6 '14 at 22:41
• Ok. I probably don't have time this week, but when I'm less busy I'll try to find a proof that your generation method produces the same table as mine, because I think it would be a good addition to the stuff I've written up already. – Peter Taylor Oct 7 '14 at 7:24
• Actually it's quite simple: its equivalence to my calculation method comes down to the lemma that the conjugate of the multiset-union of two partitions is equal to the pointwise sum of their conjugates. – Peter Taylor Oct 9 '14 at 8:20
• (slaping my head) But of course! Why didn't I think of that earlier? :-) – Florian F Oct 9 '14 at 15:24

# ksh/zsh, M = 126

In this simple system, each spy throws binary digits to the other spy. In each throw, the first stone is ignored, the next stones are each bit 0, and the last stone is bit 1. For example, to throw 20, a spy would throw 4 stones (ignore, 0, 2, add 4), then throw 3 stones (ignore, 8, add 16), because 4 + 16 = 20.

The set of numbers is not contiguous. 0 to 126 are in, but 127 is out. (If both spies have 127, they need 28 stones, but they have 26 stones.) Then 128 to 158 are in, 159 is out, 160 to 174 are in, 175 is out, 176 to 182 are in, 183 is out, 184 to 186 is in, 187 is out, and so on.

Run an automatic swap with ksh spy.sh 125 126, or run individual spies with ksh spy.sh spy1 125 and ksh spy.sh spy2 126. Here, ksh can be ksh93, pdksh or zsh.

EDIT 14 Jun 2014: Fix a problem with some co-processes in zsh. They would idle forever and fail to exit, until the user killed them.

(( stones = 26 ))

# Initialize each spy.
spy_init() {
(( wnum = $1 )) # my number (( rnum = 0 )) # number from other spy (( rlog = -1 )) # exponent from other spy } # Read stone count from other spy. spy_read() { read count || exit (( stones -= count )) # Ignore 1 stone. (( count > 1 )) && { # Increment exponent. Add bit to number. (( rlog += count - 1 )) (( rnum += 1 << rlog )) } } # Write stone count to other spy. spy_write() { if (( wnum )) then # Find next set bit. Prepare at least 2 stones. (( count = 2 )) until (( wnum & 1 )) do (( wnum >>= 1 )) (( count += 1 )) done (( wnum >>= 1 )) # Remove this bit. (( stones -= count )) print$count      # Throw stones.
else
# Throw 1 stone for other spy to ignore.
(( stones -= 1 ))
print 1
fi
}

# spy1 writes first.
spy1() {
spy_init "$1" while (( stones )) do spy_write (( stones )) || break spy_read done print$rnum
}

spy2() {
spy_init "$1" while (( stones )) do spy_read (( stones )) || break spy_write done print$rnum
}

(( $# == 2 )) || { name=${0##*/}
print -u2 "usage: $name number1 number2" print -u2 " or:$name spy[12] number"
exit 1
}

case "$1" in spy1) spy1 "$2"
exit;;
spy2)
spy2 "$2" exit;; esac (( number1 =$1 ))
(( number2 = $2 )) if [[ -n$KSH_VERSION ]]
then
eval 'cofork() { "$@" |& }' elif [[ -n$ZSH_VERSION ]]
then
# In zsh, a co-process stupidly inherits its own >&p, so it never
# reads end of file.  Use 'coproc :' to close <&p and >&p.
eval 'cofork() {
coproc {
coproc :
"$@" } }' fi # Fork spies in co-processes. [[ -n$KSH_VERSION ]] && eval 'coproc() { "$@" |& }' cofork spy1 number1 exec 3<&p 4>&p cofork spy2 number2 exec 5<&p 6>&p check_stones() { (( stones -= count )) if (( stones < 0 )) then print -u2 "$1 is in trouble! " \
"Needs $count stones, only had$((stones + count))."
exit 1
else
print "$1 threw$count stones.  Pile has $stones stones." fi } # Relay stone counts while spies throw stones. while (( stones )) do # First, spy1 writes to spy2. read -u3 count report1 || mia spy1 check_stones spy1 print -u6$count

(( stones )) || break

# Next, spy2 writes to spy1.
read -u5 count report2 || mia spy2
check_stones spy2
print -u4 $count done mia() { print -u2 "$1 is missing in action!"
exit 1
}

read -u3 report1 || mia spy1
read -u5 report2 || mia spy2

pass=true
(( number1 != report2 )) && {
print -u2 "FAILURE: spy1 put $number1, but spy2 got$report2."
pass=false
}
(( number2 != report1 )) && {
print -u2 "FAILURE: spy2 put $number2, but spy1 got$report1."
pass=false
}

if $pass then print "SUCCESS: spy1 got$report1, spy2 got \$report2."
exit 0
else
exit 1
fi