]> www.fi.muni.cz Git - aoc.git/blob - 2022/17.pl
Day 9: quite tedious, resembling real work :-)
[aoc.git] / 2022 / 17.pl
1 #!/usr/bin/perl -w
2
3 use v5.36;
4 use strict;
5 use experimental 'multidimensional';
6
7 my %seen = ("0,0" => 1);
8 my ($x, $y, $tx, $ty) = (0, 0, 0, 0);
9
10 while (<>) {
11         my ($dir, $len) = /^(.) (\d+)/;
12
13         while ($len--) {
14                 if ($dir eq 'U') {
15                         $y--;
16                 } elsif ($dir eq 'D') {
17                         $y++;
18                 } elsif ($dir eq 'L') {
19                         $x--;
20                 } elsif ($dir eq 'R') {
21                         $x++;
22                 }
23                 
24                 if ($x == $tx) {
25                         if ($y == $ty - 2) {
26                                 $ty--;
27                         } elsif ($y == $ty + 2) {
28                                 $ty++;
29                         }
30                 } elsif ($y == $ty) {
31                         if ($x == $tx - 2) {
32                                 $tx--;
33                         } elsif ($x == $tx + 2) {
34                                 $tx++;
35                         }
36                 } elsif ($x == $tx - 2) {
37                         $tx--;
38                         if ($y > $ty) {
39                                 $ty++;
40                         } else {
41                                 $ty--
42                         }
43                 } elsif ($x == $tx + 2) {
44                         $tx++;
45                         if ($y > $ty) {
46                                 $ty++;
47                         } else {
48                                 $ty--;
49                         }
50                 } elsif ($y == $ty - 2) {
51                         $ty--;
52                         if ($x > $tx) {
53                                 $tx++;
54                         } else {
55                                 $tx--;
56                         }
57                 } elsif ($y == $ty + 2) {
58                         $ty++;
59                         if ($x > $tx) {
60                                 $tx++;
61                         } else {
62                                 $tx--;
63                         }
64                 }
65                 $seen{"$tx,$ty"}++;
66                 say "$x,$y $tx,$ty";
67         }
68 }
69
70 say scalar keys %seen;
71