summary refs log tree commit diff homepage
path: root/2023/day11.pl
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2023-12-19 15:49:44 -0500
committerJune McEnroe <june@causal.agency>2023-12-19 15:49:44 -0500
commit203deeb2565c55273f016771cc1ff7042dcb784a (patch)
tree643531f215c2fe11cdd3f6aa315874bd739ba0b7 /2023/day11.pl
parentSolve day 11 part 1 (diff)
downloadaoc-master.tar.gz
aoc-master.zip
Solve day 11 part 2 HEAD master
Diffstat (limited to '')
-rw-r--r--2023/day11.pl47
1 files changed, 26 insertions, 21 deletions
diff --git a/2023/day11.pl b/2023/day11.pl
index b9996b7..4beb54b 100644
--- a/2023/day11.pl
+++ b/2023/day11.pl
@@ -16,29 +16,34 @@ while (<>) {
 	$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;
+sub expand {
+	my ($f, @gs) = @_;
+	my ($xe, $ye) = (0, 0);
+	for (my $x = 0; $x < $w; ++$x) {
+		next if defined $xp[$x];
+		for (@gs) {
+			$_ = [ $_->[0] + $f-1, $_->[1] ] if $_->[0] > ($x+$xe);
+		}
+		$xe += $f-1;
 	}
-	$x++;
-	$w++;
-}
-for ($y = 0; $y < $h; ++$y) {
-	next if defined $yp[$y];
-	splice @yp, $y, 0, undef;
-	for (@gs) {
-		$_->[1]++ if $_->[1] > $y;
+	for (my $y = 0; $y < $h; ++$y) {
+		next if defined $yp[$y];
+		for (@gs) {
+			$_ = [ $_->[0], $_->[1] + $f-1 ] if $_->[1] > ($y+$ye);
+		}
+		$ye += $f-1;
 	}
-	$y++;
-	$h++;
+	@gs;
 }
-my $sum = 0;
-while (@gs) {
-	my $a = shift @gs;
-	for my $b (@gs) {
-		$sum += abs($a->[0] - $b->[0]) + abs($a->[1] - $b->[1]);
+sub dists {
+	my $sum = 0;
+	while (@_) {
+		my $a = shift;
+		for my $b (@_) {
+			$sum += abs($a->[0] - $b->[0]) + abs($a->[1] - $b->[1]);
+		}
 	}
+	$sum;
 }
-print $sum, "\n";
+print dists(expand(2, @gs)), "\n";
+print dists(expand(1000000, @gs)), "\n";