]> www.fi.muni.cz Git - aoc.git/blobdiff - 2019/05.pl
First half of Year 2019
[aoc.git] / 2019 / 05.pl
diff --git a/2019/05.pl b/2019/05.pl
new file mode 100755 (executable)
index 0000000..72fb699
--- /dev/null
@@ -0,0 +1,35 @@
+#!/usr/bin/perl -w
+
+use v5.16;
+
+$; = ',';
+sub walk {
+       my ($desc) = @_;
+       my %rv;
+       my ($x, $y) = (0, 0);
+       for my $step (split /,/, $desc) {
+               my ($dir, $dist) = $step =~ /([A-Z])(\d+)/;
+               my ($dx, $dy);
+               ($dx, $dy) = (1,  0) if $dir eq 'R';
+               ($dx, $dy) = (0,  1) if $dir eq 'D';
+               ($dx, $dy) = (0, -1) if $dir eq 'U';
+               ($dx, $dy) = (-1, 0) if $dir eq 'L';
+               $rv{$x += $dx, $y += $dy} = 1 while $dist--;
+               say "$step -> $x, $y";
+       }
+       return \%rv;
+}
+
+my $p1 = walk(scalar <>);
+my $p2 = walk(scalar <>);
+
+my $dist;
+for my $pos (keys %$p1) {
+       next if !$p2->{$pos};
+       my ($x, $y) = split /,/, $pos;
+       my $d = abs($x)+abs($y);
+       $dist = $d if !$dist || $dist > $d;
+}
+
+say $dist;
+