This commit is contained in:
Yanick Champoux 2017-12-20 10:56:50 -05:00
parent 105cb86d44
commit 88422a98ed
3 changed files with 1075 additions and 0 deletions

38
20/1.pl Normal file
View File

@ -0,0 +1,38 @@
use 5.20.0;
use List::AllUtils qw/ sum /;
use List::UtilsBy qw/ nsort_by /;
use DDP;
my $i;
my @raw = map { [ map { [ split ',' ] } /[pva]=<(.*?)>/g ] } <>;
my @p = map { [ map { manhat(@$_) } @$_ ] } @raw;
for ( sort {
$p[$a][2] <=> $p[$b][2]
or
$p[$a][1] <=> $p[$b][1]
or
$p[$a][0] <=> $p[$b][0]
} 0..$#p ) {
# p $raw[$_];
p $p[$_];
say $_;
exit if $i++ >= 3;
}
sub manhat { sum map { abs } @_ }
p @p;
my( $min ) = sort {
$p[$a][2] <=> $p[$b][2]
or
$p[$a][1] <=> $p[$b][1]
or
$p[$a][0] <=> $p[$b][0]
} 0..$#p;
say $min;

37
20/2.pl Normal file
View File

@ -0,0 +1,37 @@
use 5.20.0;
use List::AllUtils qw/ sum uniq /;
use List::UtilsBy qw/ nsort_by /;
use experimental qw/ postderef smartmatch /;
use DDP;
my $i;
my @raw = map { [ map { [ split ',' ] } /[pva]=<(.*?)>/g ] } <>;
while() {
check_collisions();
move_particles();
say scalar @raw;
}
sub move_particles {
for ( @raw ) {
for my $i ( 0..2 ) {
$_->[1][$i] += $_->[2][$i];
$_->[0][$i] += $_->[1][$i];
}
}
}
sub check_collisions {
my %seen;
my @to_delete;
for ( 0..$#raw ) {
my $k = join ':', $raw[$_][0]->@*;
push @to_delete, $seen{$k}, $_ if exists $seen{$k};
$seen{$k} = $_;
}
return unless @to_delete;
@raw = @raw[ grep { not $_ ~~ @to_delete } 0..$#raw ];
}

1000
20/input.txt Normal file

File diff suppressed because it is too large Load Diff