Skip to content

Instantly share code, notes, and snippets.

@zachwhalen
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachwhalen/ec320a3e315d2f108987 to your computer and use it in GitHub Desktop.
Save zachwhalen/ec320a3e315d2f108987 to your computer and use it in GitHub Desktop.
Work in progress code for generating all possible panel layouts
#!/usr/bin/local/perl
#use experimental 'smartmatch';
no if $] >= 5.017011, warnings => 'experimental::smartmatch';
# Can I do this with 2x4?
# ##
# ##
# ##
# ##
# So
# AB
# CD
# EF
# GH
$maxRow = 2;
$maxCol = 4;
#$allpoints = join('',A..P);
#for ($t = 0; $t < 100000; $t++){
while (1){
open READ, "twobyfourouts.txt" or die "couldn't find it";
@already = <READ>;
close READ;
%have = {};
foreach (@already){
chomp;
$have{$_} = 'got';
}
my $allpoints = 'ABCDEFGH';
#@rows = ('ABCD','EFGH','IJKL','MNOP');
#@columns = ('AEIM','BFJN','CGKO','DHLP');
my $rows = 'ABCDEFGH';
my $columns = 'ACEGBDFH';
#$layout = ''; # eventually a string because it's easier to read
# like
# A1.3;B2.3;D1.4
# start
my %layout;
#foreach (keys %layout) {print $_ ."\n"; }
#last;
my $layoutstring = '';
LOOK: while (length($allpoints) > 0){
my $point = substr($allpoints,int(rand(length $allpoints)),1);
my ($px,$py) = (index($rows,$point) % $maxRow, index($columns,$point) % $maxCol);
# print "$point = $px,$py\n";
my $target = substr($allpoints,int(rand(length $allpoints)),1);
my ($tx,$ty) = (index($rows,$target) % $maxRow, index($columns,$target) % $maxCol);
# print "$target = $tx,$ty\n";
# can we draw a valid rectangle with these two points?
# valid means that every point between actually exists
my @xrange = sort($tx,$px);
my @yrange = sort($ty,$py);
my $width = abs($tx - $px);
my $height = abs($ty - $py);
my ($tlX, $brX) = sort($tx,$px);
my ($tlY, $brY) = sort($ty,$py);
my $tlLetter = substr($rows,$tlY * $maxRow + $tlX,1);
#print "tlLetter = $tlLetter\n";
my $rectString = $tlLetter . ($width + 1) . ":" . ($height + 1) . ";";
# print $rectString . "\n";
my $remove = '';
for ($w = 0; $w < $maxRow;$w++){
for ($h = 0; $h < $maxCol; $h++){
# is this between my two points?
# and is this letter still available?
my $thisletter = substr($rows,$h * $maxRow + $w,1);
if ($w ~~ [$xrange[0]..$xrange[1]] && $h ~~ [$yrange[0]..$yrange[1]]){
if (index($allpoints,$thisletter) == -1){
# print "Overlap detected. Trying again....\n";
next LOOK;
}else{
#continue
# print "$thisletter is a match.\n";
#remove it
$remove .= $thisletter;
}
}
}
}
foreach (split//,$remove){
substr($allpoints,index($allpoints,$_),1,"");
}
# print "Saving layout string\n\n";
# print "allpoints eq $allpoints\n\n";
$layout{$tlLetter} = ($width + 1) . ":" . ($height + 1);
#last;
}
foreach (sort keys %layout){
$layoutstring .= "$_$layout{$_}";
#print "$_ => $layout{$_}\n";
}
if ($have{$layoutstring} eq 'got'){
#print "Already found this one, moving on...\n";
}else{
print "Generated layout: $layoutstring\n";
open WRITE, ">>twobyfourouts.txt" or die "wat";
print WRITE $layoutstring . "\n";
}
close WRITE;
}
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment