summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2021-12-12 12:38:09 -0500
committerJune McEnroe <june@causal.agency>2021-12-12 12:38:09 -0500
commitcc8f474aeca07cebe23fa60c153c84b6a256f2e0 (patch)
treec1316b2909c50b1c52af317a6a7df5f9557fcec6
parentSolve day 11 part 2 (diff)
downloadaoc-cc8f474aeca07cebe23fa60c153c84b6a256f2e0.tar.gz
aoc-cc8f474aeca07cebe23fa60c153c84b6a256f2e0.zip
Solve day 12 part 1
-rw-r--r--2021/day12.pl27
1 files changed, 27 insertions, 0 deletions
diff --git a/2021/day12.pl b/2021/day12.pl
new file mode 100644
index 0000000..ceef3e0
--- /dev/null
+++ b/2021/day12.pl
@@ -0,0 +1,27 @@
+use strict;
+use warnings;
+my %edges;
+while (<>) {
+	/(\w+)-(\w+)/;
+	$edges{$1} //= [];
+	$edges{$2} //= [];
+	push @{$edges{$1}}, $2;
+	push @{$edges{$2}}, $1;
+}
+my $paths = 0;
+my @queue = (['start']);
+while (@queue) {
+	my @path = @{shift @queue};
+	my %visited = map { $_ => 1 } @path;
+	for (@{$edges{$path[0]}}) {
+		if ($_ eq 'end') {
+			$paths++;
+		} elsif (/[a-z]/ && $visited{$_}) {
+			next;
+		} else {
+			my @next = ($_, @path);
+			push @queue, \@next;
+		}
+	}
+}
+print "$paths\n";