diff options
-rw-r--r-- | 2021/day12.pl | 21 |
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"; |