#!/usr/bin/perl
#
#  attempt to find optimal packing for
#  a digit sequence that must contain each
#  of '00'..'99' at least once, assuming
#  that leading zeroes are not permitted
#
#  (theoretical lower limit on length =101)
#
#  rattled out one sleepless night by
#    Frank Wales (frank@limov.com)
#

foreach $start (1..9) {
    $current=''.$start;

    foreach $i (0..99) {
	$available{sprintf('%02d',$i)}=1;
    }

    NEXTDIGIT:
    while (scalar(keys %available)) {

	foreach $maybe (keys %available) {
	    delete $available{$maybe} if ($current=~/$maybe/);
	}

	$lastdigit=substr($current,length($current)-1,1);

	foreach $try (0..9) {
	    next unless $available{$lastdigit.$try};

	    $current.=$try;

	    delete($available{$lastdigit.$try});

	    next NEXTDIGIT;
	}

	$try=(sort(keys %available))[0];
	$current.=$try;

	delete($available{try});
    }

    print STDOUT "Start: $start, result length=".length($current).", value=$current\n";
}
