-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.pl
188 lines (170 loc) · 3.72 KB
/
10.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
#!/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 pairs mesh zip/;
use Math::Cartesian::Product;
use Math::Complex;
use List::PriorityQueue;
use Memoize;
use Term::ANSIColor qw(:constants);
use Storable qw(dclone);
use utf8::all;
# use POSIX;
sub out {
my $out = shift;
if (ref($out)) {
print Dumper($out);
} else {
Clipboard->copy_to_all_selections("./submit.py $out");
print "$out\n";
}
}
# Print and array highlighting some cells
# Args:
# - array ref, row, col, row, col, ...
# - array ref, "row,col", "row,col", ...
# - array ref, array of [row, col, row, col, ...]
# - array ref, array of ["row,col", "row,col", ...]
# - array ref, array of [[row, col, color?], ...]
# - array ref, array of [["row,col", color?], ...]
sub hilite {
my $arr = shift;
my @hilite = @_;
# If neighbor array ref, deref
if (@hilite == 1 && ref($hilite[0])) {
@hilite = @{$hilite[0]};
}
# If the array is raw coords turn into array of [row, col]
if (@hilite && !ref($hilite[0])) {
my $h1 = $hilite[0];
if ($h1 =~ /^\d+,\d+$/o) {
# "row,col"
@hilite = map {/^(\d+),(\d+)$/o; [$1,$2]} @hilite;
} else {
# row, col, row, col
@hilite = pairs(@hilite);
}
}
my %hilite;
for my $h (@hilite) {
my ($r, $c, $v) = @$h;
$hilite{"$r,$c"}=($v || BOLD . ON_RED);
}
my $maxlen = 0;
for my $r (@$arr) {
for my $c (@$r) {
$maxlen=length($c) if length($c) > $maxlen;
}
}
for my $r (0..$#$arr) {
my $ra = $arr->[$r];
for my $c (0..$#$ra) {
my $v = $ra->[$c];
print $hilite{"$r,$c"} || '';
printf("%${maxlen}s", $v);
print RESET;
}
print "\n";
}
print "\n";
}
my $ADDSPACE = 1;
# Parse input
my @A;
while (<>) {
chomp;
last unless $_;
if ($ADDSPACE) {
push @A, [map {($_,'-')} split('')];
push @A, [('|',' ') x length($_)];
} else {
push @A, [split('')];
}
}
# Utility constants
no warnings 'qw';
my %DIRS = qw/D 1,0 L 0,-1 U -1,0 R 0,1/;
my %CHARS = qw/| DDUU - LLRR L LUDR 7 RDUL F URLD J RUDL/;
my %RCHARS;
for my $k (keys %CHARS) {
my ($a,$b,$c,$d) = split ('', $CHARS{$k});
delete $CHARS{$k};
$CHARS{$k}{$a} = $b;
$CHARS{$k}{$c} = $d;
$RCHARS{"$a$b"} = $k;
$RCHARS{"$c$d"} = $k;
}
# Find start
my ($sr,$sc);
for my $r (0..$#A) {
for my $c (0..$#{$A[0]}) {
if ($A[$r][$c] eq 'S') {
($sr,$sc) = ($r,$c);
}
}
}
# Find loop
my %l;
DLOOP: for my $sd (qw/L D U/) {
my ($row,$col) = ($sr,$sc);
my $d = $sd;
%l=();
while (!$l{"$row,$col"}) {
$l{"$row,$col"}=$d;
my $dir = [split(',', $DIRS{$d})];
$row+=$dir->[0]; $col+=$dir->[1];
my $ch = $A[$row][$col];
last if ($ch eq 'S');
unless ($CHARS{$ch}{$d}) {
next DLOOP;
}
$d = $CHARS{$ch}{$d};
}
# Clean up the "S"
$A[$row][$col] = $RCHARS{"$d$sd"};
last DLOOP;
}
# Output part 1
#hilite(\@A,keys %l);
out (scalar(%l)/($ADDSPACE?4:2));
# Scan for "inside"
my @l;
my $sum;
for my $r (0..$#A) {
my $out = 1;
for my $c (0..$#{$A[0]}) {
my $ch = $l{"$r,$c"} ? $A[$r][$c] : 0;
if ($ch =~ /[|7F]/) {
$out=!$out;
}
if (!$out && !$ch) {
push @l,"$r,$c";
unless ($ADDSPACE && ($r&1 || $c&1)) {
$sum++;
}
}
}
die unless $out;
}
# Output part 2
#hilite(\@A,@l);
out($sum);
if ($ADDSPACE) {
for my $r (0..$#A) {
for my $c (0..$#{$A[0]}) {
if (!$l{"$r,$c"} && ($r&1 || $c&1)) {
$A[$r][$c] = ' ';
}
}
}
}
@A = map {[map {tr/.LFJ7|-/·└┌┘┐│─/;$_} @$_]} @A;
#viz
hilite(
\@A,
(map {[split(',',$_), ON_RED]} (keys %l)),
(map {[split(',',$_), ON_BLUE]} @l),
[$sr,$sc,BOLD.ON_GREEN]);