Created
September 30, 2013 03:02
-
-
Save touzoku/6758912 to your computer and use it in GitHub Desktop.
Silly Japanese homework bruteforce tool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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