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 Math::Vector::Real;
- my ($vy,$vx) = Math::Vector::Real->canonical_base(2);
- my @Dirs = ($vy, $vx, -$vy, -$vx);
- # Read in grid, adding sentinel ~s to right and bottom
- my @Grid = map { chomp; [split(//), '~'] } <>;
- push( @Grid, [('~') x $Grid[0]->@*] );
- sub grid_at ($) { my $p = shift; return ($Grid[$p->[0]][$p->[1]]) }
- sub print_grid { say "\t", join( '', @$_ ) foreach (@Grid); }
- my $part1 = 0;
- my $part2 = 0;
- my %visited;
- foreach my $y (0 .. $#Grid - 1) {
- foreach my $x (0 .. $Grid[0]->$#* - 1) {
- my $start = V($y,$x);
- next if ($visited{$start});
- my $plant = grid_at($start);
- my $area = 0;
- my $inner = 0;
- my @queue = ($start);
- my %neigh;
- while (my $pos = shift @queue) {
- next if ($visited{$pos});
- my @field;
- foreach my $d (@Dirs) {
- my $p = $pos + $d;
- if (grid_at($p) eq $plant) {
- push( @field, $p);
- } else {
- push( $neigh{$p}->@*, $d );
- }
- }
- $inner += scalar @field;
- $area++;
- $visited{$pos}++;
- push( @queue, @field);
- }
- my $perm = 4 * $area - $inner;
- $part1 += $area * $perm;
- my %corners;
- foreach my $n (keys %neigh) {
- my $pos = V($n =~ m#([-0-9]+)#g);
- foreach my $d ($neigh{$n}->@*) {
- my $left = V( -$d->[1], $d->[0] );
- my $corn = $pos;
- $corn += $left while (exists $neigh{$corn} and grid_at($corn - $d) eq $plant);
- $corn -= $left;
- if (!exists $corners{$corn,$d}) {
- $corners{$corn,$d}++;
- }
- }
- }
- my $sides = scalar keys %corners;
- $part2 += $area * $sides;
- }
- }
- say "Part 1: $part1";
- say "Part 2: $part2";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement