## Using Maple 15 to Solve a Peg Board Puzzle Game

Maple 15 In high school I was briefly fascinated by a triangular "jump all but one" game, commonly found at Cracker Barrel restaurants.  The basic premise is that any peg can "jump" over an adjacent peg to occupy the empty hole next to the jumped peg.  The jumped peg is then removed.  The goal is to continue jumping pegs until there is only one left.

The instructions on the face of the Cracker Barrel version of this game say, "LEAVE ONLY ONE -- YOU'RE A GENIUS".  Wanting to claim the right to call myself a genius, unlike ordinary kids, who might just play the game a few times, I sat down on my Turbo-XT and started writing BASIC code.  The algorithm I came up with ran a bit slow, so I directed output to my printer and let it run over night.  In the morning the program was still chugging along.  I advanced the paper feed on the dot-matrix lineprinter -- the kind that used continuous feed paper with perforated edges and holes on each side.   Into view came 3 solutions represented by a string of numbers.   A quick check verified that I was now a genius.

With the addition of Maple 15's new parallel multi-process features, and beefed up Grid Computing package, I was recently thinking about big examples that could show off how fast Maple can run on modern multi-core computers.  My mind turned back to this toy game, and I wondered just how long it would take to find a solution in Maple?

It turns out that a brute force algorithm for finding solutions is fairly easy to write.  Here, I generalized it to a triangle with N-pegs on each side.

Peg := proc( N )
local peg, i, j, r, A;

peg := proc( A, N, path, sol )
local i, j, p;

p := [op(path),A];
for i from 1 to N do
for j from 1 to N-i+1 do
if A[i,j] then
if i+2 <= N-j+1 and A[i+1,j] and not A[i+2,j] then
peg( subsop([i,j]=false,[i+1,j]=false,[i+2,j]=true,A), N, p, sol );
end if;
if i-2 > 0 and A[i-1,j] and not A[i-2,j] then
peg( subsop([i,j]=false,[i-1,j]=false,[i-2,j]=true,A), N, p, sol );
end if;

if i-2 > 0 and j+2 <= N-i+3 and A[i-1,j+1] and not A[i-2,j+2] then
peg( subsop([i,j]=false,[i-1,j+1]=false,[i-2,j+2]=true,A), N, p, sol );
end if;
if j-2 > 0 and i+2 <= N-j+3 and A[i+1,j-1] and not A[i+2,j-2] then
peg( subsop([i,j]=false,[i+1,j-1]=false,[i+2,j-2]=true,A), N, p, sol );
end if;

if j+2 <= N-i+1 and A[i,j+1] and not A[i,j+2] then
peg( subsop([i,j]=false,[i,j+1]=false,[i,j+2]=true,A), N, p, sol );
end if;
if j-2 > 0 and A[i,j-1] and not A[i,j-2] then
peg( subsop([i,j]=false,[i,j-1]=false,[i,j-2]=true,A), N, p, sol );
end if;

end if;
end do;
end do;
if numelems(p) = N*(N+1)/2-1 then
sol(numelems(sol)+1) := p;
end if;
end proc:

A := [seq([seq(true,j=1..N-i+1)],i=1..N)]; #fill all holes
A[1,1] := false; #pick first spot as empty
r := Array(1..0);
peg(A,N,[],r);
r;
end proc: The Cracker Barrel version had 5 pegs on each edge.   Running this in serial, Maple finds 29760 solutions in just under a minute.  That's the number of solutions when starting with a corner peg empty. > t := time[real]():
 > R := Peg(5);
 > time[real]() - t; (1) Each entry in the array,  R, stores an encoding of the solution.  So you don't have to decode it, below is a simple procedure to animate any given solution.  Click the plot generated and press the play button to see how to proceed through a given solution. PegPlot := proc( moves, N )
local pegframe, i;

pegframe := proc( A, N )
local i, j, p1, p2;

#plot board
p1 := plots:-pointplot( [
seq(seq([(i-N)/2+j,N-i],j=1..N-i+1),i=1..N)
],symbol=circle,symbolsize=31,axes=none);

#plot pegs
p2 := plots:-pointplot( [
seq(seq(`if`(A[i,j],[(i-N)/2+j,N-i],NULL),j=1..N-i+1),i=1..N)
],symbol=solidcircle,symbolsize=30,color=blue,axes=none);

plots[display]([p2,p1]);
end proc;

plots:-display([seq(pegframe(moves[i],N),i=1..numelems(moves))],insequence=true);

end proc:

> PegPlot(R,5); The algorithm for finding solutions is brute-force.  It searches through every possible combination of moves in a recursive way.  The outlook for parallelizing the code seemed pretty good.  An initial version which just split each of the N  starting points off onto another process immediately cut the speed in half.  This was a pretty good result for doing almost no work.  Looking at the balance of work being done, I saw that this split wasn't balanced.  One node was doing half the work, while the other three cores were long finished.  After some tweaking I came up with the following: ParallelPeg := proc( N )

local peg, i, j, r, holes, nholes, me, numnodes, A;

peg := proc( A, N, path, sol, lev )
local i, j, p, k1, k2, h, split;

p := [op(path),A];

if lev = 6 then
me := Grid:-MyNode();
numnodes := Grid:-NumNodes();
nholes := numelems(holes);
split := holes[(me)*floor(nholes/numnodes) + 1
.. `if`(me=numnodes-1,nholes,(me+1)*floor(nholes/numnodes))];
#printf("Node %d level %d split: %La\n",Grid:-MyNode(),lev,split); >
else
split := holes;
end if;

for h in split do
i,j := op(h);
if A[i,j] then
if i+2 <= N-j+1 and A[i+1,j] and not A[i+2,j] then
peg( subsop([i,j]=false,[i+1,j]=false,[i+2,j]=true,A), N, p, sol, lev+1 );
end if;
if i-2 > 0 and A[i-1,j] and not A[i-2,j] then
peg( subsop([i,j]=false,[i-1,j]=false,[i-2,j]=true,A), N, p, sol, lev+1 );
end if;

if i-2 > 0 and j+2 <= N-i+3 and A[i-1,j+1] and not A[i-2,j+2] then
peg( subsop([i,j]=false,[i-1,j+1]=false,[i-2,j+2]=true,A), N, p, sol, lev+1 );
end if;
if j-2 > 0 and i+2 <= N-j+3 and A[i+1,j-1] and not A[i+2,j-2] then
peg( subsop([i,j]=false,[i+1,j-1]=false,[i+2,j-2]=true,A), N, p, sol, lev+1 );
end if;

if j+2 <= N-i+1 and A[i,j+1] and not A[i,j+2] then
peg( subsop([i,j]=false,[i,j+1]=false,[i,j+2]=true,A), N, p, sol, lev+1 );
end if;
if j-2 > 0 and A[i,j-1] and not A[i,j-2] then
peg( subsop([i,j]=false,[i,j-1]=false,[i,j-2]=true,A), N, p, sol, lev+1 );
end if;

end if;
end do;
if numelems(p) = N*(N+1)/2-1 then
sol(numelems(sol)+1) := p;
end if;
NULL;
end proc:

printf("Node %d called with N = %a\n",Grid:-MyNode(),N);
holes := [seq(seq([i,j],j=1..N-i+1),i=1..N)];

A := [seq([seq(true,j=1..N-i+1)],i=1..N)]; #fill all holes
A[1,1] := false; #pick first spot as empty
r := Array(1..0);
peg(A,N,[],r,1);

printf("Node %d found %d solutions\n",Grid:-MyNode(),numelems(r));
if Grid:-MyNode() = 0 then
else
Grid:-Send(0,r);
end if;
r;
end proc:

 > t := time[real]():
 > R2 := Grid:-Launch(ParallelPeg,5,numnodes=5);
 > time[real]()-t;
 Node 3 called with N = 5 Node 2 called with N = 5 Node 1 called with N = 5 Node 4 called with N = 5 Node 0 called with N = 5 Node 3 found 3456 solutions Node 2 found 2925 solutions Node 1 found 7309 solutions Node 0 found 7969 solutions Node 4 found 8101 solutions (2)

I ran this using 5 processes computing on a 4-core cpu.  The balance of work was better, with no node doing more than about 30% of the work for the 5-peg triangle.  The run time went from 47 seconds down to 19 seconds.  The important thing to note was that the code stayed mostly the same.

I haven't had the patience to run a 6-peg triangle using this code yet.  Maybe I should tie the output to my printer?
{"ft_lang_label":"__label__en","ft_lang_prob":0.7828701,"math_prob":0.99171823,"size":6968,"snap":"2021-43-2021-49","text_gpt3_token_len":2377,"char_repetition_ratio":0.13885698,"word_repetition_ratio":0.25577265,"special_character_ratio":0.35490814,"punctuation_ratio":0.22159383,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9987562,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-20T23:44:53Z\",\"WARC-Record-ID\":\"<urn:uuid:8e34ff03-517e-4d0a-90bb-1a3ab262e4af>\",\"Content-Length\":\"170239\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:9a3501c1-0f2d-4c9f-b479-b47620adc224>\",\"WARC-Concurrent-To\":\"<urn:uuid:4f3e58c4-3a4a-49ac-b62e-4f1ae18e6e94>\",\"WARC-IP-Address\":\"199.71.183.11\",\"WARC-Target-URI\":\"https://mapleprimes.com/maplesoftblog/119099-Using-Maple-15-To-Solve-A-Peg-Board-Puzzle-Game\",\"WARC-Payload-Digest\":\"sha1:6P2LCGFTR3H7C2UC5FEQXSKYGATDSWXT\",\"WARC-Block-Digest\":\"sha1:LKULC2OVGACJEBZAZWHHVNACVTSTH3OC\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585353.52_warc_CC-MAIN-20211020214358-20211021004358-00246.warc.gz\"}"}