6 my $serial = shift @ARGV;
11 return $cache{$x,$y} if defined $cache{$x,$y};
13 my $rack_id = $x + 10;
14 my $power = ($rack_id * $y + $serial) * $rack_id;
15 $power = "000" . $power;
16 $power =~ s/.*(.)..\z/$1/;
18 return $cache{$x,$y} = $power;
22 my ($maxx, $maxy, $maxsize, $maxpwr);
24 my ($x, $y, $sizex, $sizey) = @_;
26 return $sq_cache{$x,$y,$sizex,$sizey}
27 if defined $sq_cache{$x,$y,$sizex,$sizey};
30 if ($sizex == 1 && $sizey == 1) {
31 $sum = power_at($x, $y);
32 } elsif ($sizex == 1) {
33 $sum = square_at($x, $y, $sizex, $sizey-1)
34 + power_at($x, $y+$sizey-1);
35 } elsif ($sizey == 1) {
36 $sum = square_at($x, $y, $sizex-1, $sizey)
37 + power_at($x+$sizex-1, $y);
39 $sum = square_at($x, $y, $sizex-1, $sizey-1)
40 + square_at($x+$sizex-1, $y, 1, $sizey-1)
41 + square_at($x, $y+$sizey-1, $sizex-1, 1)
42 + power_at($x+$sizex-1, $y+$sizey-1);
44 if ($sizex == $sizey && (!defined $maxpwr || $sum > $maxpwr)) {
50 $sq_cache{$x,$y,$sizex,$sizey} = $sum;
53 for my $size (1 .. 300) {
54 for my $x (1 .. 301-$size) {
55 for my $y (1 .. 301-$size) {
56 my $sz = square_at($x, $y, $size, $size);
57 # say "$x,$y,$size = $sz";
60 say "size $size, $maxx,$maxy,$maxsize = $maxpwr";
63 say "$maxx,$maxy,$maxsize = $maxpwr";