From b402fe77fa5e96fc832bbadf8f57491cc8fccd78 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Sun, 12 Dec 2021 12:57:09 -0500 Subject: Solve day 12 part 2 --- 2021/day12.pl | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to '2021') 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"; -- cgit 1.4.1