-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.pl
213 lines (190 loc) · 3.76 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/perl -w
use strict;
no warnings 'portable';
use Data::Dumper;
use feature 'say';
use Clipboard;
use List::Util qw/sum min max reduce any all none notall first product uniq/;
use Math::Cartesian::Product;
use Math::Complex;
use List::PriorityQueue;
use Memoize;
use Storable qw(dclone);
sub out {
my $out = shift;
if (ref($out)) {
print Dumper($out);
} else {
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 neigh_arr, arr, row, col
# OR: neigh_arr, arr, "row,col"
# OR: neigh_arr, rows, cols, row, col
# OR: neigh_arr, rows, cols, "row,col"
# returns array of [row, col, value]
# OR: array of ["row,col", value]
# OR: array of "row,col"
sub neigh {
my $neigh = shift;
my ($rows,$cols);
my $arr = shift;
if (ref $arr) {
$rows = @$arr;
$cols = @{$arr->[0]};
} else {
$rows = $arr;
$cols = shift;
undef $arr;
}
my $row = shift;
my $col = shift;
my $comma;
if ($row =~ /(\d+)(\D+)(\d+)/) {
($row, $comma, $col) = ($1, $2, $3);
}
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;
if (defined($comma)) {
if ($arr) {
push @out, ["$nr$comma$nc", $arr->[$nr][$nc]];
} else {
push @out, "$nr$comma$nc";
}
} else {
push @out, [$nr, $nc, $arr ? ($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);
}
sub hex2bin {
my $in = shift;
return join('', map {sprintf("%04b", oct("0x$_"))} split('', $in));
}
sub bin2hex {
my $in = shift;
my $out;
my @in = split('', $in);
die "Bad bin value $in" if (@in%4);
while (@in) {
$out .= sprintf("%X", bin2dec(join('',splice(@in, 0, 4))));
}
return $out;
}
sub smart_split {
my $str = shift || $_;
return ($str =~ m{[a-zA-Z]+|\d+}go);
}
sub hashify {
my @arr = ref $_[0] ? @{$_[0]} : @_;
return map {$_ => 1} @arr;
}
sub arrify {
my $x = $_[0];
if (ref($x) ne 'ARRAY') {
return [$x];
} else {
return $x;
}
}
sub cmpl {
my ($a,$b) = @_;
if (ref($a) ne 'ARRAY' && ref($b) ne 'ARRAY') {
return $a <=> $b;
}
$a = arrify($a);
$b = arrify($b);
my $m = $#$a;
$m = $#$b if ($#$b < $m);
for my $i (0..$m) {
my $c = cmpl($a->[$i], $b->[$i]);
if ($c) {
return $c;
}
}
return $#$a <=> $#$b;
}
memoize(\&cmpl);
my %H;
my $sum=0;
my $i=0;
my @A = ([[2]],[[6]]);
while (<>) {
chomp;
next unless $_;
$i++;
my $a = eval($_);
$_=<>;
my $b = eval($_);
$sum+=$i if (cmpl($a,$b) == -1);
push @A, $a, $b;
}
out($sum);
@A = sort {cmpl($a,$b)} @A;
my $p=1;
sub strify {
my $x = shift;
if (ref($x) eq 'ARRAY') {
return '['.join(',', map {strify($_)} @$x).']';
} else {
return $x;
}
}
for my $i (0..$#A) {
if (strify($A[$i]) eq '[[2]]' || strify($A[$i]) eq '[[6]]') {
$p *= $i+1;
}
}
out ($p);