-
-
Save alexgreenbank/6ca5a459f46a72f064d6b2bd3f4450a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/perl | |
| use strict; | |
| my %map=(); | |
| while( my $line = <STDIN> ) { | |
| chomp( $line ); | |
| my ( $f, $t ) = split( /-/, $line ); | |
| push( @{$map{$f}}, $t ) if( $t ne "start" ); | |
| push( @{$map{$t}}, $f ) if( $f ne "start" ); | |
| } | |
| my $routes=0; | |
| foreach my $poss ( @{$map{'start'}} ) { | |
| my %visited=(); go( $poss, \%visited, 1 ); | |
| } | |
| print "part1: $routes\n"; | |
| $routes=0; | |
| foreach my $poss ( @{$map{'start'}} ) { | |
| my %visited=(); go( $poss, \%visited, 0 ); | |
| } | |
| print "part2: $routes\n"; | |
| exit; | |
| sub go { | |
| my ( $where, $v, $vtwice ) = @_; | |
| $v->{$where}++ if( $where eq lc($where) ); | |
| foreach my $poss ( @{$map{$where}} ) { | |
| if( $poss eq "end" ) { $routes++; next; } | |
| next if( exists( $v->{$poss} ) && $vtwice ); | |
| my %visited=%$v; | |
| go( $poss, \%visited, exists( $v->{$poss} ) ? 1 : $vtwice ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment