]> www.fi.muni.cz Git - aoc.git/blob - 2019/05.pl
Day 25: examining the input
[aoc.git] / 2019 / 05.pl
1 #!/usr/bin/perl -w
2
3 use v5.16;
4
5 $; = ',';
6 sub walk {
7         my ($desc) = @_;
8         my %rv;
9         my ($x, $y) = (0, 0);
10         for my $step (split /,/, $desc) {
11                 my ($dir, $dist) = $step =~ /([A-Z])(\d+)/;
12                 my ($dx, $dy);
13                 ($dx, $dy) = (1,  0) if $dir eq 'R';
14                 ($dx, $dy) = (0,  1) if $dir eq 'D';
15                 ($dx, $dy) = (0, -1) if $dir eq 'U';
16                 ($dx, $dy) = (-1, 0) if $dir eq 'L';
17                 $rv{$x += $dx, $y += $dy} = 1 while $dist--;
18                 say "$step -> $x, $y";
19         }
20         return \%rv;
21 }
22
23 my $p1 = walk(scalar <>);
24 my $p2 = walk(scalar <>);
25
26 my $dist;
27 for my $pos (keys %$p1) {
28         next if !$p2->{$pos};
29         my ($x, $y) = split /,/, $pos;
30         my $d = abs($x)+abs($y);
31         $dist = $d if !$dist || $dist > $d;
32 }
33
34 say $dist;
35