#!perl use strict; use warnings; use Max::Bins; my $max = $ARGV[0] || 30; my @can = sort {$a <=> $b} map {int(rand $max) + 1} 1 .. 10; my @work; my $next = progressive($max, @can); while (my @group = $next->()) { my ($pick, $left) = avail(\@can, $max, @group); next if ! $pick; push @work, {left => $left, depth => 1, path => $pick}; } my ($path, $water_mark) = ('', 1); while (@work) { my $choice = pop @work; my @can = @{$choice->{left}}; my $next = progressive($max, @can); while (my @group = $next->()) { my ($pick, $left) = avail(\@can, $max, @group); next if ! $pick; my $depth = $choice->{depth} + 1; my $way = $choice->{path} . ":$pick"; ($path, $water_mark) = ($way, $depth) if $depth > $water_mark; push @work, {left => $left, depth => $depth, path => $way} if @$left + $depth > $water_mark; } } print $path;