%begin
%integer nslots,nprobes
  %cycle
    prompt("Slots:"); read(nslots); skipsymbol
    %exitif nslots<=1
    prompt("Probes:"); read(nprobes); skipsymbol
    %exitif nprobes<2 %or nprobes>nslots
    %begin
    %bytearray b(1:nslots,1:nslots)
    %bytearray score,visits,p(1:nslots),q(1:nprobes)
    %integer lastprobe = nprobes, lastslot = nslots
    %integer moves=0,total,i,j
      %for i = 1,1,nslots %cycle
        b(i,j) = 0 %for j = 1,1,nslots
        p(i) = 0; visits(i) = 0
      %repeat
      p(i) = i %and q(i) = i %for i = 1,1,nprobes
      %cycle
! work out score and show configuration
        %for i = 1,1,nprobes %cycle
          %for j = 1,1,nprobes %cycle
            b(q(i),q(j)) = 1
          %repeat
        %repeat
        total = 0
        %for i = 1,1,nslots %cycle
          score(i) = 0
          %for j = 1,1,nslots %cycle
            score(i) = score(i)+1 %if b(i,j)=0
          %repeat
          total = total+score(i)
        %repeat
!       write(i,2) %for i = 1,1,nslots; printstring(" slot"); newline
!       write(p(i),2) %for i = 1,1,nslots; printstring(" probe"); newline
!       write(score(i),2) %for i = 1,1,nslots; printstring(" score")
        write(moves,3); write(total,6); %exitif total=0; printsymbol(13)
! choose which probe to move next
        %integerfn best probe
        %integer best=0,bestval=maxint,i
          %for i = 1,1,nprobes %cycle
            lastprobe = lastprobe+1; lastprobe = 1 %if lastprobe>nprobes
            %if score(q(lastprobe))<bestval %start
              bestval = score(q(lastprobe)); best = lastprobe
            %finish
          %repeat
          lastprobe = best
          %result = best
        %end
        %integerfn best slot
        %integer best=0,bestval=-1,i
          %for i = 1,1,nslots %cycle
            lastslot = lastslot+1; lastslot = 1 %if lastslot>nslots
            %if p(lastslot)=0 %and score(lastslot)>bestval %start
              bestval = score(lastslot); best = lastslot
            %finish
          %repeat
          lastslot = best
          %result = best
        %end
        %routine move(%integer probe,slot)
!         printstring("Moving probe"); write(probe,1)
!         printstring(" to slot"); write(slot,1); newline
          p(q(probe)) = 0; q(probe) = slot; p(slot) = probe
          moves = moves+1
        %end
        move(bestprobe,bestslot)
      %repeat
      newline
    %end
  %repeat
%end
