Skip to content

Instantly share code, notes, and snippets.

@kga
Created Jul 12, 2010
Embed
What would you like to do?
#!/usr/bin/env perl
use strict;
use warnings;
use Perl6::Say;
use List::MoreUtils qw(minmax);
chomp (my @input = <DATA>);
my @direct = ([-1, 0], [0, -1], [1, 0], [0, 1]);
while (my $n = shift @input) {
my @data;
for (1..$n-1) {
push @data, shift @input;
}
my ($x, $y) = set(@data);
if (scalar $n > 1) {
my ($min_x, $max_x) = minmax @$x;
my ($min_y, $max_y) = minmax @$y;
say sprintf '%d %d', ($max_x - $min_x) + 1, ($max_y - $min_y) + 1;
} else {
say '1 1';
}
}
sub set {
my @data = @_;
my @sq_x = 0;
my @sq_y = 0;
for my $data (@data) {
my ($n, $d) = split /\s/, $data;
my $new = $direct[$d];
push @sq_x, $sq_x[$n] + $new->[0];
push @sq_y, $sq_y[$n] + $new->[1];
}
return (\@sq_x, \@sq_y);
}
__DATA__
1
5
0 0
0 1
0 2
0 3
12
0 0
1 0
2 0
3 1
4 1
5 1
6 2
7 2
8 2
9 3
10 3
10
0 2
1 2
2 2
3 2
2 1
5 1
6 1
7 1
8 1
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment