This commit is contained in:
Yanick Champoux 2018-12-07 11:26:56 -05:00
parent 5d5e3f49f7
commit d1f5e7b34c
3 changed files with 148 additions and 0 deletions

50
2018/06/input.txt Normal file
View File

@ -0,0 +1,50 @@
275, 276
176, 108
270, 134
192, 224
252, 104
240, 271
144, 220
341, 303
344, 166
142, 347
207, 135
142, 353
343, 74
90, 210
82, 236
124, 295
41, 226
298, 109
276, 314
50, 303
131, 42
119, 335
275, 125
113, 289
347, 230
192, 329
158, 316
154, 356
171, 350
165, 59
257, 129
306, 55
334, 203
55, 63
268, 198
44, 103
230, 199
41, 181
357, 328
331, 85
256, 290
168, 290
353, 77
81, 328
136, 316
138, 213
352, 271
139, 222
139, 318
194, 239

92
2018/06/sol1.pl Normal file
View File

@ -0,0 +1,92 @@
use 5.20.0;
use warnings;
use List::AllUtils qw/ minmax pairs uniq /;
use List::UtilsBy qw/ max_by /;
use experimental qw/
signatures
postderef
/;
my @points = map { [ split ', ' ] } <>;
my @range_x = minmax map { $_->[0] } @points;
my @range_y = minmax map { $_->[1] } @points;
use DDP;
p @range_x;
p @range_y;
my @grid;
my $id = 'a';
my @infinite;
my @movers = map {
$id++;
[ $id, 0, @$_ ]
} @points;
while( my $next = shift @movers ) {
calculate_distances( @$next );
}
push @infinite,
uniq
map { eval { $_->[1]->@* } }
grep { $_ and 1 == $_->[1]->@* }
map { $grid[$_]->@* } $range_x[0]-1, $range_x[1]+1;
my %tally;
for my $x ( map { eval { $_->[1] } } map { eval { @$_ } } @grid ) {
next unless ref $x;
next if 1 < @$x;
$tally{ $x->[0] }++;
}
delete @tally{@infinite};
my $max = max_by { $_->[1] } pairs %tally;
p $max;
# for my $row ( @grid ) {
# say map { $_ ?
# ($_->[1]->@* > 1 ? ' ' : $_->[1]->@* ) : '' } @$row
# }
sub calculate_distances($id, $distance, $x, $y) {
if( $x < $range_x[0] - 1
or $x > $range_x[1] +1
or $y < $range_y[0] - 1
or $y > $range_y[1] +1 ) { return; }
if( !$grid[$x][$y] ) {
$grid[$x][$y] = [ $distance, [ $id ] ];
}
else {
if( $distance < $grid[$x][$y][0] ) {
$grid[$x][$y] = [ $distance, [ $id ] ];
}
elsif( $distance == $grid[$x][$y][0] ) {
if( grep { $_ eq $id } $grid[$x][$y][1]->@* ) { return }
push $grid[$x][$y][1]->@*, $id;
return;
}
else {
return;
}
}
$distance++;
push @movers,
[ $id, $distance, $x+1, $y ],
[ $id, $distance, $x-1, $y ],
[ $id, $distance, $x, $y+1 ],
[ $id, $distance, $x, $y-1 ];
}

6
2018/06/test.txt Normal file
View File

@ -0,0 +1,6 @@
1, 1
1, 6
8, 3
3, 4
5, 5
8, 9