]> www.fi.muni.cz Git - aoc.git/blob - 2019/06.pl
Day 25: examining the input
[aoc.git] / 2019 / 06.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         my $l = 0;
11         for my $step (split /,/, $desc) {
12                 my ($dir, $dist) = $step =~ /([A-Z])(\d+)/;
13                 my ($dx, $dy);
14                 ($dx, $dy) = (1,  0) if $dir eq 'R';
15                 ($dx, $dy) = (0,  1) if $dir eq 'D';
16                 ($dx, $dy) = (0, -1) if $dir eq 'U';
17                 ($dx, $dy) = (-1, 0) if $dir eq 'L';
18                 while ($dist--) {
19                         $x += $dx; $y += $dy; ++$l;
20                         $rv{$x, $y} //= $l;
21                 }
22                 say "$step -> $x, $y ($rv{$x,$y})";
23         }
24         return \%rv;
25 }
26
27 my $p1 = walk(scalar <>);
28 my $p2 = walk(scalar <>);
29
30 my $dist;
31 for my $pos (keys %$p1) {
32         next if !$p2->{$pos};
33         next if $pos eq '0,0';
34         my $d = $p1->{$pos}+$p2->{$pos};
35         say "Intersection at $pos dist $d";
36         $dist = $d if !$dist || $dist > $d;
37 }
38
39 say $dist;
40