From 04bf386cfdd6e1152596bf613cbafdf0f0299a3c Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Fri, 9 Dec 2022 06:28:32 +0100 Subject: [PATCH] Day 9: quite tedious, resembling real work :-) --- 2022/17.pl | 71 ++++++++++++++++++++++++++++++++++++++++++++++ 2022/18.pl | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100755 2022/17.pl create mode 100755 2022/18.pl diff --git a/2022/17.pl b/2022/17.pl new file mode 100755 index 0000000..63fa9de --- /dev/null +++ b/2022/17.pl @@ -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 index 0000000..6c4c7eb --- /dev/null +++ b/2022/18.pl @@ -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; + -- 2.43.0