Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use utf8;
- use strict;
- use warnings;
- use v5.26;
- use List::AllUtils qw(pairwise all true);
- use Math::Utils qw(sign);
- sub vecsum (\@\@) {
- my ($v1,$v2) = @_;
- return (pairwise {$a + $b} @$v1, @$v2);
- }
- sub veceq (\@\@) {
- my ($v1,$v2) = @_;
- return (all {$v1->[$_] == $v2->[$_]} (0 .. @$v1 - 1));
- }
- my %grid;
- while (<>) {
- my ($x1,$y1,$x2,$y2) = split /\D+/;
- my @Δ = sign($x2 - $x1, $y2 - $y1);
- my @p = ($x1, $y1);
- my @e = vecsum( @{[$x2,$y2]}, @Δ );
- until (veceq( @p, @e )) {
- $grid{$p[0],$p[1]}++;
- @p = vecsum( @p, @Δ );
- }
- }
- print "Part 2: ", (true {$grid{$_} >= 2} keys %grid), "\n";
Add Comment
Please, Sign In to add comment