-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.pl
128 lines (117 loc) · 2.42 KB
/
13.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
use feature 'say';
use Clipboard;
use List::Util qw/sum min max/;
use Math::Cartesian::Product;
use Math::Complex;
sub out {
my $out = shift;
Clipboard->copy_to_all_selections($out);
print "$out\n";
}
# Utility function to make sure 2d array has equal rows
# input arr,fill. no output
sub equalize {
my $arr = shift;
my $fill = shift;
my $rows = @$arr;
my $cols = max(map {$_ ? scalar(@$_): 0} @$arr);
print "$rows $cols\n";
for my $row (@$arr) {
$row = [] unless $row;
while (@$row < $cols) {
push @$row, $fill;
}
}
}
# Find neighbors
# input arr, row, col, neigh_arr
# returns array of [row, col, value]
sub neigh {
my $arr = shift;
my $row = shift;
my $col = shift;
my $neigh = shift;
my $rows = @$arr;
my $cols = @{$arr->[$row]};
my @out;
for my $pair (@$neigh) {
my ($rd, $cd) = @$pair;
my ($nr, $nc) = ($row + $rd, $col + $cd);
next if $nr < 0;
next if $nc < 0;
next if $nr >= $rows;
next if $nc >= $cols;
push @out, [$nr, $nc, $arr->[$nr][$nc]];
}
return @out;
}
# Orthogonal
sub oneigh {
return neigh(@_, [[-1,0], [1, 0], [0, -1], [0, 1]]);
}
# All neighbors
sub aneigh {
return neigh(@_, [
[-1, -1], [-1, 0], [-1, 1],
[ 0, -1], [ 0, 1],
[ 1, -1], [ 1, 0], [ 1, 1]]);
}
# Numeric sort because sort defaults to lex
# returns new array
sub nsort {
my $in = \@_;
if (@$in == 1) {
$in = $in->[0];
}
return sort {$a <=> $b} @$in;
}
# Binary conversions
sub bin2dec {
my $in = shift;
return oct("0b$in");
}
sub dec2bin {
my $in = shift;
return sprintf ("%b", $in);
}
my %A;
ROW: while(<>) {
chomp;
last unless $_;
m{(\d+),(\d+)}go;
$A{"$1,$2"}++;
}
while (<>) {
my $sum = 0;
m{fold along ([xy])=(\d+)}o;
my ($d,$n) = ($1, $2);
my %O;
for my $i (keys %A) {
$i =~ m{(\d+),(\d+)}o;
my ($x, $y) = ($1,$2);
my ($sx, $sy) = (\$x, \$y);
($sx, $sy) = ($sy, $sx) if ($d eq 'y');
if ($$sx > $n) {
$$sx = 2*$n - $$sx;
}
unless ($O{"$x,$y"}++) {
$sum++;
}
}
out ($sum);
%A = %O;
}
my $minx = min(map {m{(\d+),(\d+)}o; $1} keys(%A));
my $maxx = max(map {m{(\d+),(\d+)}o; $1} keys(%A));
my $miny = min(map {m{(\d+),(\d+)}o; $2} keys(%A));
my $maxy = max(map {m{(\d+),(\d+)}o; $2} keys(%A));
say "$minx $miny $maxx $maxy";
for my $y ($miny..$maxy) {
for my $x ($minx..$maxx) {
print ($A{"$x,$y"} ? '#' : '.');
}
print "\n";
}