-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16.pl
64 lines (56 loc) · 1.21 KB
/
16.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
#!/usr/bin/perl -w
use strict;
no warnings 'portable';
use feature 'say';
use List::Util qw/max/;
$|=1;
my @A;
while (my $line = <>) {
chomp $line;
last unless $line;
push @A, [split('', $line)];
}
my $rows = scalar(@A);
my $cols = scalar(@{$A[0]});
sub try {
my @OPEN=@_;
my %CLOSED;
my %SEEN;
while (@OPEN) {
my $x = shift @OPEN;
next if ($CLOSED{$x});
$CLOSED{$x}++;
my ($r,$c,$rd,$cd) = split(',', $x);
$SEEN{"$r,$c"}++;
my $ch = $A[$r][$c];
if ($ch eq '/') {
($rd,$cd) = (-$cd,-$rd);
} elsif ($ch eq '\\') {
($rd,$cd) = ($cd,$rd);
} elsif ($ch eq '|' && $cd) {
push @OPEN,"$r,$c,-1,0","$r,$c,1,0";
next;
} elsif ($ch eq '-' && $rd) {
push @OPEN,"$r,$c,0,-1","$r,$c,0,1";
next;
}
$r+=$rd; $c+=$cd;
next if ($r<0 || $c<0 || $r >= $rows || $c >= $cols);
push @OPEN,"$r,$c,$rd,$cd";
}
return scalar(%SEEN);
}
my $sum=0;
my $part1;
for my $r (0..$rows-1) {
$sum = max($sum,try("$r,0,0,1"));
unless ($part1) {
say($sum); $part1++;
}
$sum = max($sum,try("$r,".($cols-1).",0,-1"));
}
for my $c (0..$cols-1) {
$sum = max($sum,try("0,$c,1,0"));
$sum = max($sum,try(($rows-1).",$c,-1,0"));
}
say($sum);