Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- my %Graph;
- # read in graph
- while (<>) {
- my ($a, $b) = m#(start|end|..)-(start|end|..)#;
- push( @{$Graph{$a}}, $b );
- push( @{$Graph{$b}}, $a );
- }
- sub recurse_path {
- my ($loc, $double, %visit) = @_;
- my $ret = 0;
- # reached end, return 1 way
- return (1) if ($loc eq 'end');
- # handle small cave... set double if second time here
- if ($loc eq lc($loc)) {
- $visit{$loc}++;
- $double = 1 if ($visit{$loc} == 2);
- }
- # recurse to valid neighbours
- foreach my $neigh (@{$Graph{$loc}}) {
- next if ($neigh eq 'start'); # only once ever
- next if (exists $visit{$neigh} && $double); # this would be a second double
- $ret += &recurse_path( $neigh, $double, %visit );
- }
- return ($ret);
- }
- print "Part 2: ", &recurse_path( 'start', 0 ), "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement