Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use v5.26;
- use warnings;
- use List::AllUtils qw(all indexes true);
- use Math::Vector::Real;
- # Read in grid
- my @Grid = map { chomp; [split(//)] } <>;
- my $Size = scalar( @Grid ); # ASSUME: square grid
- # Parse Grid: get blank rows, cols, and galaxies
- my @Galaxies;
- my @Blank_rows;
- my @Blank_cols;
- foreach my $i (0 .. $Size - 1) {
- push (@Blank_rows, $i) if (all { $_ eq '.' } $Grid[$i]->@*);
- push (@Blank_cols, $i) if (all { $_ eq '.' } map { $Grid[$_][$i] } (0 .. $Size - 1));
- push( @Galaxies, map { V($i,$_) } indexes { $_ eq '#' } $Grid[$i]->@* );
- }
- sub expand_universe {
- my $scale = $_[0] - 1; # we already have the first lines
- my @shifted = map {
- my $g = $_;
- my $yshift = true { $g->[0] > $_ } @Blank_rows;
- my $xshift = true { $g->[1] > $_ } @Blank_cols;
- $g += V($yshift, $xshift) * $scale;
- } @Galaxies;
- my $dist = 0;
- foreach my $i (0 .. $#shifted-1) {
- foreach my $j ($i+1 .. $#shifted) {
- $dist += $shifted[$i]->manhattan_dist( $shifted[$j] );
- }
- }
- return ($dist);
- }
- say "Part 1: ", &expand_universe( 2 );
- say "Part 2: ", &expand_universe( 1_000_000 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement