summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2023-12-19 15:22:06 -0500
committerJune McEnroe <june@causal.agency>2023-12-19 15:22:06 -0500
commitfc712b474f56c861d7a5deaac29a364086517f1a (patch)
treea7dc87b898a62125ed078bb9fa691dbdf56f6e4c
parentSolve day 9 part 2 (diff)
downloadaoc-fc712b474f56c861d7a5deaac29a364086517f1a.tar.gz
aoc-fc712b474f56c861d7a5deaac29a364086517f1a.zip
Solve day 11 part 1
-rw-r--r--2023/day11.pl44
1 files changed, 44 insertions, 0 deletions
diff --git a/2023/day11.pl b/2023/day11.pl
new file mode 100644
index 0000000..b9996b7
--- /dev/null
+++ b/2023/day11.pl
@@ -0,0 +1,44 @@
+use strict;
+use warnings;
+my (@gs, @xp, @yp);
+my ($x, $y) = (0, 0);
+while (<>) {
+	chomp;
+	$x = 0;
+	for (split //) {
+		if ($_ eq "#") {
+			push @gs, [ $x, $y ];
+			$xp[$x] = 1;
+			$yp[$y] = 1;
+		}
+		$x++;
+	}
+	$y++;
+}
+my ($w, $h) = ($x, $y);
+for ($x = 0; $x < $w; ++$x) {
+	next if defined $xp[$x];
+	splice @xp, $x, 0, undef;
+	for (@gs) {
+		$_->[0]++ if $_->[0] > $x;
+	}
+	$x++;
+	$w++;
+}
+for ($y = 0; $y < $h; ++$y) {
+	next if defined $yp[$y];
+	splice @yp, $y, 0, undef;
+	for (@gs) {
+		$_->[1]++ if $_->[1] > $y;
+	}
+	$y++;
+	$h++;
+}
+my $sum = 0;
+while (@gs) {
+	my $a = shift @gs;
+	for my $b (@gs) {
+		$sum += abs($a->[0] - $b->[0]) + abs($a->[1] - $b->[1]);
+	}
+}
+print $sum, "\n";