From 9e2ac1101f0c865e0c6e07e02213313ff75e625b Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Mon, 11 Dec 2023 06:21:59 +0100 Subject: [PATCH] Day 11: not bad, part 2 was easy --- 2023/21.pl | 36 ++++++++++++++++++++++++++++++++++++ 2023/22.pl | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100755 2023/21.pl create mode 100755 2023/22.pl diff --git a/2023/21.pl b/2023/21.pl new file mode 100755 index 0000000..bfc568a --- /dev/null +++ b/2023/21.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl -w + +use v5.38; + +my @gxys; +my $y = 0; +while (<>) { + while (/#/g) { + my $x = pos()-1; + push @gxys, [ $x, $y ]; + } + $y++; +} + +my %x_gxy = map { $_->[0] => 1 } @gxys; +my %y_gxy = map { $_->[1] => 1 } @gxys; + +my $steps; +for my $i (0 .. $#gxys-1) { + for my $j ($i .. $#gxys) { + my ($x1, $y1) = $gxys[$i]->@*; + my ($x2, $y2) = $gxys[$j]->@*; + + ($x1, $x2) = ($x2, $x1) if $x1 > $x2; + for ($x1+1 .. $x2) { + $steps += $x_gxy{$_} ? 1 : 2; + } + + ($y1, $y2) = ($y2, $y1) if $y1 > $y2; + for ($y1+1 .. $y2) { + $steps += $y_gxy{$_} ? 1 : 2; + } + } +} + +say $steps; diff --git a/2023/22.pl b/2023/22.pl new file mode 100755 index 0000000..83c91c0 --- /dev/null +++ b/2023/22.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl -w + +use v5.38; + +my @gxys; +my $y = 0; +while (<>) { + while (/#/g) { + my $x = pos()-1; + push @gxys, [ $x, $y ]; + } + $y++; +} + +my %x_gxy = map { $_->[0] => 1 } @gxys; +my %y_gxy = map { $_->[1] => 1 } @gxys; + +my $steps; +for my $i (0 .. $#gxys-1) { + for my $j ($i .. $#gxys) { + my ($x1, $y1) = $gxys[$i]->@*; + my ($x2, $y2) = $gxys[$j]->@*; + + ($x1, $x2) = ($x2, $x1) if $x1 > $x2; + for ($x1+1 .. $x2) { + $steps += $x_gxy{$_} ? 1 : 1_000_000; + } + + ($y1, $y2) = ($y2, $y1) if $y1 > $y2; + for ($y1+1 .. $y2) { + $steps += $y_gxy{$_} ? 1 : 1_000_000; + } + } +} + +say $steps; -- 2.43.5