-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14.pl
302 lines (281 loc) · 6.12 KB
/
14.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/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;
}
# A* / BFS implementation
# Args: start, end, neighbor function, heuristic function
# - start is either a node or a list of nodes to start at.
# - end is either a node or a function that takes a single node
# and returns true/false.
# - neighbor function: node -> ([new_node, cost], ...)
# - cost assumed 1 if missing
# - heuristic function: node -> lower bound on cost to end (optional)
sub astar {
my ($start, $end, $neigh, $h) = @_;
# Generalize parameters
$start = [$start] unless (ref($start) eq 'ARRAY');
if (ref($end) ne 'CODE') {
my $end_node = $end;
$end = sub { return $_[0] eq $end_node; };
}
die "bad neigh func $neigh" unless (ref($neigh) eq 'CODE');
$h = sub {return 0;} unless $h;
# Initialize open list
my $OPEN = new List::PriorityQueue;
my %gscore;
my %OHASH;
for my $s (@$start) {
$gscore{$s} = 0;
$OHASH{$s} = 1;
$OPEN->insert($s, $h->($s));
}
my %path;
my $it;
while (%OHASH) {
$it--;
my $cur = $OPEN->pop();
delete $OHASH{$cur};
# Check for end
if ($end->($cur)) {
say "reached end at $cur";
my $score = $gscore{$cur};
return $score unless wantarray;
my @path = ($cur);
while ($cur = $path{$cur}) {
unshift(@path, $cur)
}
return ($score, @path);
}
# Expand neighbors
for my $n ($neigh->($cur)) {
my ($np,$v);
if (ref($n) eq 'ARRAY') {
($np,$v) = @$n;
} else {
$np = $n;
}
if (!defined($v)) {
$v = 1;
}
my $new_g = $gscore{$cur} + $v;
if (!exists($gscore{$np}) || $new_g < $gscore{$np}) {
# Found better path to $np
$path{$np} = $cur if wantarray;
$gscore{$np} = $new_g;
my $fscore = $new_g + $h->($np);
if (!$OHASH{$np}) {
$OPEN->insert($np, $fscore);
$OHASH{$np}++;
} else {
$OPEN->update($np, $fscore);
}
}
}
}
return $it;
}
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;
}
my @A;
my %H;
my $sum=0;
my $maxy=0;
my $maxx=0;
while (<>) {
chomp;
last unless $_;
my ($x,$y,@p) = smart_split();
while (@p) {
$maxx = $x if ($x > $maxx);
$maxy = $y if ($y > $maxy);
my $nx = shift @p;
my $ny = shift @p;
if ($nx == $x) {
my ($y1,$y2) = ($ny < $y) ? ($ny,$y) : ($y,$ny);
for my $yy ($y1..$y2) {
$H{$x,$yy}='#';
}
} else {
my ($x1,$x2) = ($nx < $x) ? ($nx,$x) : ($x,$nx);
for my $xx ($x1..$x2) {
$H{$xx,$y}='#';
}
}
($x,$y) = ($nx,$ny);
}
$maxx = $x if ($x > $maxx);
$maxy = $y if ($y > $maxy);
}
sub nnn {
my $coord = shift;
my ($x,$y) = split($;, $coord);
return () if ($y == $maxy+1);
$y++;
my @n = ("$x$;$y", ($x-1)."$;$y", ($x+1)."$;$y");
my @o;
for my $n (@n) {
push @o, $n unless $H{$n};
}
return @o;
}
out(-astar("500".$;."0", 0, \&nnn));
my $p1;
BIG: while (!$H{500,0}) {
my ($x,$y) = (500,0);
while (1) {
$y++;
if ($y==$maxy+2) {
unless ($p1) {
out($sum);
$p1=1;
}
last;
}
if (!$H{$x,$y}) {
next;
} elsif (!$H{$x-1,$y}) {
$x--;
} elsif (!$H{$x+1,$y}) {
$x++;
} else {
last;
}
}
$maxx = $x if ($x > $maxx);
$H{$x,$y-1} = '*';
$sum++;
}
#for my $y (0..$maxy+2) {
# for my $x (0..$maxx) {
# print $H{$x,$y} || ' ';
# }
# print "\n";
#}
out ($sum);