summary refs log tree commit diff homepage
diff options
context:
space:
mode:
-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";
00'>2019-10-24Unset non-blocking on clientsJune McEnroe I figure I'll actually use POLLOUT on clients so should never have issues. 2019-10-24Intercept client QUITJune McEnroe 2019-10-24Only set NeedCapEnd if unregisteredJune McEnroe 2019-10-24Set client-side origin to irc.invalidJune McEnroe 2019-10-24Factor out client-side origin nameJune McEnroe 2019-10-24Intercept all CAP commands from clientsJune McEnroe 2019-10-24Ignore further CAP ENDJune McEnroe 2019-10-24Implement client CAP for server-timeJune McEnroe 2019-10-24Implement ringWriteJune McEnroe 2019-10-24Make clientFormat publicJune McEnroe 2019-10-24Make serverFormat publicJune McEnroe 2019-10-24Rename some thingsJune McEnroe 2019-10-23Add ISUPPORT draft to STANDARDSJune McEnroe 2019-10-23Track own originJune McEnroe 2019-10-23Track channels and sync ISUPPORTJune McEnroe 2019-10-23Track nick changesJune McEnroe 2019-10-23Rename Command to MessageJune McEnroe 2019-10-23Synchronize state after client registrationJune McEnroe 2019-10-23Send to server if client has no needsJune McEnroe 2019-10-23Implement some amount of client connectionJune McEnroe 2019-10-23Set clients non-blockingJune McEnroe 2019-10-23Clean up state.c and factor out parsingJune McEnroe 2019-10-23Respond to pingsJune McEnroe 2019-10-23Add verbose flagJune McEnroe 2019-10-23Set NOSIGPIPE on server connectionJune McEnroe 2019-10-23Set an initial loop capJune McEnroe 2019-10-23Fix rest parsingJune McEnroe 2019-10-23Add dynamic poll listJune McEnroe 2019-10-23Don't assume commands have targets and handle ERRORJune McEnroe 2019-10-23Clean up state somewhatJune McEnroe 2019-10-23Actually send the buffer...June McEnroe 2019-10-23Add stateJune McEnroe