The file format is:
# X-size, Y-size, Number of these x,y,n
The code follows:
use strict; use warnings; use IO::File; ###################################################################### +########## my $fn = shift; my $fh = IO::File->new($fn) || die "Cannot open '$fn' for reading: $!\n"; my @squares; while (<$fh>) { chomp; my ($x, $y, $num) = split ','; ($x,$y) = ($y,$x) if $x > $y; push @squares, [ $x, $y, $x * $y ] for 1 .. $num; } $fh->close; print "Working with:\n"; print "\t$_->[0] x $_->[1]\n" for @squares; try(@squares); @$_[0,1] = @$_[1,0] for @squares; print "Working with:\n"; print "\t$_->[0] x $_->[1]\n" for @squares; try(@squares); ###################################################################### +########## sub try { my @squares = @_; @squares = sort { $b->[2] <=> $a->[2] || $a->[0] <=> $b->[0] || $b->[1] <=> $a->[1] } @squares; my $cur_x = 0; my $cur_y = 0; my $max_y = $squares[0][1]; my $cur_p_x = 0; my $cur_p_y = 0; my $max_p_y = 0; my @placement; my @layout; foreach my $square (@squares) { my ($x, $y) = @$square; for my $i ($cur_x .. $cur_x + $x - 1) { $layout[$i] += $y; } $placement[$cur_p_x][$cur_p_y] = $square; $cur_y += $y; if ($cur_y >= $max_y) { $cur_x += $x; $cur_y = 0; $cur_p_x++; $cur_p_y = 0; } else { $cur_p_y++; $max_p_y = $cur_p_y if $max_p_y < $cur_p_y; } } for (@layout) { $max_y = $_ if $max_y < $_; } my $waste = 0; for (@layout) { $waste += $max_y - $_; } my $max_x = scalar @layout; print "Square is $max_x x $max_y or ", $max_x * $max_y, " big +with a waste of $waste\n"; print "Layout:\n"; for my $j (0 .. $max_p_y) { foreach my $square (map { $_->[$j] || [] } @placement) { unless (@$square) { print "\t"; next; } print "$square->[0] x $square->[1]\t"; } print "\n"; } }
------
We are the carpenters and bricklayers of the Information Age.
The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6
Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.
In reply to Re: help with nesting
by dragonchild
in thread help with nesting
by stu96art
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |