Skip to content

Instantly share code, notes, and snippets.

@touzoku
Created September 30, 2013 03:02
Show Gist options
  • Save touzoku/6758912 to your computer and use it in GitHub Desktop.
Save touzoku/6758912 to your computer and use it in GitHub Desktop.
Silly Japanese homework bruteforce tool
use feature 'say';
my $NUMCARDS = $ARGV[0]*2;
die "Usage: perl bruteforce.pl [NUMBER OF CARDS]\n" unless $ARGV[0];
say "num cards: $NUMCARDS";
my @init = split'','.'x$NUMCARDS;
&iterate_desc($NUMCARDS/2,@init);
#&iterate_asc(1,@init);
sub iterate_desc($@){
my $number = shift;
for (my$i=0;$i<$NUMCARDS-$number-1;$i++){
my @var = @_;
if($var[$i] eq '.' && $var[$i+$number+1] eq '.'){
# OK to put to this position
$var[$i] = $number;
$var[$i+$number+1] = $number;
# Uncomment to display permutations
#say join ' ',@var;
if($number>1){
iterate_desc($number-1,@var);
} else {
say "Solution: ",join('',@var);
}
}
}
}
# sub iterate_asc($@){
# my $number = shift;
# for (my$i=0;$i<$NUMCARDS-$number-1;$i++){
# my @var = @_;
# if($var[$i] eq '.' && $var[$i+$number+1] eq '.'){
# # OK to put to this position
# $var[$i] = $number;
# $var[$i+$number+1] = $number;
# # Uncomment to display permutations
# #say join ' ',@var;
# if($number<$NUMCARDS/2){
# iterate_asc($number+1,@var);
# } else {
# say "Solution: ",join('',@var);
# }
# }
# }
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment