diff options
| author | June McEnroe <june@causal.agency> | 2021-12-12 12:57:09 -0500 |
|---|---|---|
| committer | June McEnroe <june@causal.agency> | 2021-12-12 12:57:09 -0500 |
| commit | 72e8ea9710a447833b253cab64f189bbbbfffb4c (patch) | |
| tree | d354fd081870bbb8057d2108c6bb2d78884d1911 | |
| parent | Solve day 12 part 1 (diff) | |
| download | aoc-72e8ea9710a447833b253cab64f189bbbbfffb4c.tar.gz aoc-72e8ea9710a447833b253cab64f189bbbbfffb4c.zip | |
Solve day 12 part 2
| -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"; |