]> www.fi.muni.cz Git - aoc.git/commitdiff
Day 9: quite tedious, resembling real work :-)
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 9 Dec 2022 05:28:32 +0000 (06:28 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 9 Dec 2022 05:28:32 +0000 (06:28 +0100)
2022/17.pl [new file with mode: 0755]
2022/18.pl [new file with mode: 0755]

diff --git a/2022/17.pl b/2022/17.pl
new file mode 100755 (executable)
index 0000000..63fa9de
--- /dev/null
@@ -0,0 +1,71 @@
+#!/usr/bin/perl -w
+
+use v5.36;
+use strict;
+use experimental 'multidimensional';
+
+my %seen = ("0,0" => 1);
+my ($x, $y, $tx, $ty) = (0, 0, 0, 0);
+
+while (<>) {
+       my ($dir, $len) = /^(.) (\d+)/;
+
+       while ($len--) {
+               if ($dir eq 'U') {
+                       $y--;
+               } elsif ($dir eq 'D') {
+                       $y++;
+               } elsif ($dir eq 'L') {
+                       $x--;
+               } elsif ($dir eq 'R') {
+                       $x++;
+               }
+               
+               if ($x == $tx) {
+                       if ($y == $ty - 2) {
+                               $ty--;
+                       } elsif ($y == $ty + 2) {
+                               $ty++;
+                       }
+               } elsif ($y == $ty) {
+                       if ($x == $tx - 2) {
+                               $tx--;
+                       } elsif ($x == $tx + 2) {
+                               $tx++;
+                       }
+               } elsif ($x == $tx - 2) {
+                       $tx--;
+                       if ($y > $ty) {
+                               $ty++;
+                       } else {
+                               $ty--
+                       }
+               } elsif ($x == $tx + 2) {
+                       $tx++;
+                       if ($y > $ty) {
+                               $ty++;
+                       } else {
+                               $ty--;
+                       }
+               } elsif ($y == $ty - 2) {
+                       $ty--;
+                       if ($x > $tx) {
+                               $tx++;
+                       } else {
+                               $tx--;
+                       }
+               } elsif ($y == $ty + 2) {
+                       $ty++;
+                       if ($x > $tx) {
+                               $tx++;
+                       } else {
+                               $tx--;
+                       }
+               }
+               $seen{"$tx,$ty"}++;
+               say "$x,$y $tx,$ty";
+       }
+}
+
+say scalar keys %seen;
+
diff --git a/2022/18.pl b/2022/18.pl
new file mode 100755 (executable)
index 0000000..6c4c7eb
--- /dev/null
@@ -0,0 +1,82 @@
+#!/usr/bin/perl -w
+
+use v5.36;
+use strict;
+use experimental 'multidimensional';
+
+my %seen = ("0,0" => 1);
+my @knots;
+push @knots, [0, 0] for 1 .. 10;
+
+sub follow ($x, $y, $tx, $ty) {
+       if ($x == $tx) {
+               if ($y == $ty - 2) {
+                       $ty--;
+               } elsif ($y == $ty + 2) {
+                       $ty++;
+               }
+       } elsif ($y == $ty) {
+               if ($x == $tx - 2) {
+                       $tx--;
+               } elsif ($x == $tx + 2) {
+                       $tx++;
+               }
+       } elsif ($x == $tx - 2) {
+               $tx--;
+               if ($y > $ty) {
+                       $ty++;
+               } else {
+                       $ty--
+               }
+       } elsif ($x == $tx + 2) {
+               $tx++;
+               if ($y > $ty) {
+                       $ty++;
+               } else {
+                       $ty--;
+               }
+       } elsif ($y == $ty - 2) {
+               $ty--;
+               if ($x > $tx) {
+                       $tx++;
+               } else {
+                       $tx--;
+               }
+       } elsif ($y == $ty + 2) {
+               $ty++;
+               if ($x > $tx) {
+                       $tx++;
+               } else {
+                       $tx--;
+               }
+       }
+       ($tx, $ty);
+}
+
+while (<>) {
+       my ($dir, $len) = /^(.) (\d+)/;
+
+       while ($len--) {
+               if ($dir eq 'U') {
+                       $knots[0][1]--;
+               } elsif ($dir eq 'D') {
+                       $knots[0][1]++;
+               } elsif ($dir eq 'L') {
+                       $knots[0][0]--;
+               } elsif ($dir eq 'R') {
+                       $knots[0][0]++;
+               }
+               
+               for (1 .. $#knots) {
+                       ($knots[$_][0], $knots[$_][1]) = follow(
+                               @{ $knots[$_-1] },
+                               @{ $knots[$_] }
+                       );
+               }
+       
+               $seen{"$knots[-1][0],$knots[-1][1]"}++;
+       }
+}
+
+say scalar keys %seen;
+