Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use feature qw(say);
- use List::Util qw(max);
- use Statistics::Basic qw(variance);
- use ntheory qw(invmod);
- use constant X => 0;
- use constant Y => 1;
- my @DIM = (101, 103);
- # Robots is an array of arrays of two elements, 0 => position, 1 => velocity
- use constant POS => 0;
- use constant VEL => 1;
- my @robots = map { [map {[split(/,/, $_)]} m#([-0-9]+,[-0-9]+)#g] } <>;
- my ($minX, $varX, $minY, $varY) = (-1, ~0, -1, ~0);
- for (my $time = 1; $time <= max @DIM; $time++) {
- foreach my $i (0 .. $#robots) {
- $robots[$i][POS] = [map {($robots[$i][POS][$_] + $robots[$i][VEL][$_]) % $DIM[$_]} (X,Y)];
- }
- my $vx = variance map {$_->[POS][X]} @robots;
- my $vy = variance map {$_->[POS][Y]} @robots;
- ($minX, $varX) = ($time, $vx) if ($vx < $varX);
- ($minY, $varY) = ($time, $vy) if ($vy < $varY);
- }
- say "[$minX] $varX";
- say "[$minY] $varY";
- say "Part 2: ", $minX + ((invmod($DIM[X], $DIM[Y]) * ($minY - $minX)) % $DIM[Y]) * $DIM[X];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement