Skip to content

Instantly share code, notes, and snippets.

@ggoebel
Created December 5, 2021 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggoebel/f06857bd3b856f7bc055ac433f1e2c1f to your computer and use it in GitHub Desktop.
Save ggoebel/f06857bd3b856f7bc055ac433f1e2c1f to your computer and use it in GitHub Desktop.
use v6d;
my @input = 'input'.IO.lines;
my %grid;
my $overlap = 0;
my @diag;
for @input {
my ($x1, $y1, $x2, $y2) = .split(/\D+/, :skip-empty);
if ($x1 == $x2) {
($y1, $y2) = ($y2, $y1) if $y1 > $y2;
for $y1..$y2 -> $y {
$overlap++ if ++%grid{$x1}{$y} == 2;
}
} elsif ($y1 == $y2) {
($x1, $x2) = ($x2, $x1) if $x1 > $x2;
for $x1..$x2 -> $x {
$overlap++ if ++%grid{$x}{$y1} == 2;
}
} else {
@diag.push([$x1, $y1, $x2, $y2]);
}
}
say "Part One: $overlap";
for (@diag) {
my ($x1, $y1, $x2, $y2) = .List;
if ($x1 > $x2) {
($x1, $x2) = ($x2, $x1);
($y1, $y2) = ($y2, $y1);
}
my $y_incr = ($y1 > $y2) ?? -1 !! 1;
for ($x1 .. $x2) {
$overlap++ if ++%grid{$_}{$y1} == 2;
$y1 += $y_incr;
}
}
print "Part Two: $overlap\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment