]> www.fi.muni.cz Git - aoc.git/blob - 2017/22.pl
Day 25: examining the input
[aoc.git] / 2017 / 22.pl
1 #!/usr/bin/perl
2
3 use v5.30;
4 use strict;
5
6 my ($x, $y) = (0, 0);
7
8 my @path = split /[,\s]/, <>;
9
10 #   |/
11 #   o
12 #  /|
13 # x y
14
15 sub dist {
16         my ($x,$y) = @_;
17         my $dist;
18         if ($x >= 0 && $y >= 0 || $x <= 0 && $y <= 0) {
19                 $dist = abs($x + $y);
20         } elsif (abs($x) >= abs($y)) {
21                 $dist = abs($x);
22         } else {
23                 $dist = abs($y);
24         }
25         return $dist;
26 }
27
28 my $max;
29 for my $dir (@path) {
30         if ($dir eq 's') {
31                 $y--;
32         } elsif ($dir eq 'n') {
33                 $y++;
34         } elsif ($dir eq 'ne') {
35                 $x++;
36         } elsif ($dir eq 'nw') {
37                 $x--;
38                 $y++;
39         } elsif ($dir eq 'se') {
40                 $x++;
41                 $y--;
42         } elsif ($dir eq 'sw') {
43                 $x--;
44         }
45         my $d = dist($x,$y);
46         $max = $d if !$max || $max < $d;
47 }
48
49 say $max;
50         
51