use v5.36;
use strict;
-use experimental 'multidimensional';
+use experimental 'for_list';
my @map;
-sub sgn($x) { $x > 0 ? 1 : $x < 0 ? -1 : 0 };
-
-my $maxy;
while (<>) {
- my ($x0, $y0);
my @list = /(\d+)/g;
+ my ($x0, $y0) = splice @list, 0, 2;
+ $map[$y0][$x0] = 1;
for my ($x, $y) (@list) {
- if (!defined $x0) {
- $x0 = $x; $y0 = $y;
- $map[$y][$x] = 1;
- } else {
- while ($x0 != $x || $y0 != $y) {
- $x0 += sgn($x-$x0);
- $y0 += sgn($y-$y0);
- $map[$y0][$x0] = 1;
- $maxy = $y if !defined $maxy || $y > $maxy;
- }
+ while ($x0 != $x || $y0 != $y) {
+ $x0 += $x <=> $x0;
+ $y0 += $y <=> $y0;
+ $map[$y0][$x0] = 1;
}
}
}
+my $maxy = @map;
my $units = 0;
-UNIT:
-while (1) {
- my ($sx, $sy) = (500, 0);
-
- while (1) {
- if ($sy >= $maxy + 1) {
- $map[$sy][$sx] = 2;
- last;
- } elsif (!$map[$sy+1][$sx]) {
- $sy++;
- } elsif (!$map[$sy+1][$sx-1]) {
- $sx--;
- $sy++;
- } elsif (!$map[$sy+1][$sx+1]) {
- $sx++;
- $sy++;
+while (!$map[0][500]) {
+ my ($x, $y) = (500, 0);
+
+ while ($y < $maxy) {
+ if (!$map[$y+1][$x]) {
+ $y++;
+ } elsif (!$map[$y+1][$x-1]) {
+ $x--; $y++;
+ } elsif (!$map[$y+1][$x+1]) {
+ $x++; $y++;
} else {
- last UNIT if $map[$sy][$sx];
- $map[$sy][$sx] = 2;
last;
}
}
+ $map[$y][$x] = 2;
$units++;
- last if $sy == 0;
}
say $units;