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 ($Y,$X) = Math::Vector::Real->canonical_base(2);
- my @Dirs = ($X, $Y, -$X, -$Y);
- my $pos = V(0,0);
- my @vertices = ($pos);
- my $trench = 0;
- while (<>) {
- my ($col) = m#[UDLR] \d+ (.*)#g;
- my ($len, $dir) = ($col =~ m#([[:xdigit:]]{5})(\d)#g);
- $len = oct("0x$len");
- $trench += $len;
- push( @vertices, $pos += $len * $Dirs[$dir] );
- }
- # Shoelace for area (loop gives twice the area (sign +'ve widdershins, -'ve for deasil))
- my $area = 0;
- foreach my $i (0 .. $#vertices - 1) {
- $area += $vertices[$i][0] * $vertices[$i+1][1];
- $area -= $vertices[$i][1] * $vertices[$i+1][0];
- }
- # Pick's formula: I+B = A + B/2 + 1 (but still needed to halve the Area from shoelace)
- say "Part 2: ", (abs($area) + $trench) / 2 + 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement