summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2021-12-12 12:57:09 -0500
committerJune McEnroe <june@causal.agency>2021-12-12 12:57:09 -0500
commitb402fe77fa5e96fc832bbadf8f57491cc8fccd78 (patch)
tree32a1a1b917b0c8b407b6d471e257d96fb0cf5d4a
parentSolve day 12 part 1 (diff)
downloadaoc-b402fe77fa5e96fc832bbadf8f57491cc8fccd78.tar.gz
aoc-b402fe77fa5e96fc832bbadf8f57491cc8fccd78.zip
Solve day 12 part 2
-rw-r--r--2021/day12.pl21
1 files changed, 21 insertions, 0 deletions
diff --git a/2021/day12.pl b/2021/day12.pl
index ceef3e0..a47c9b0 100644
--- a/2021/day12.pl
+++ b/2021/day12.pl
@@ -25,3 +25,24 @@ while (@queue) {
 	}
 }
 print "$paths\n";
+$paths = 0;
+@queue = (['start']);
+while (@queue) {
+	my @path = @{shift @queue};
+	my (%visited, $twice);
+	for (@path) {
+		$twice = 1 if $visited{$_}++ && /[a-z]/;
+	}
+	for (@{$edges{$path[0]}}) {
+		next if $_ eq 'start';
+		if ($_ eq 'end') {
+			$paths++;
+		} elsif (/[a-z]/ && $visited{$_} && $twice) {
+			next;
+		} else {
+			my @next = ($_, @path);
+			push @queue, \@next;
+		}
+	}
+}
+print "$paths\n";