Skip to content

Instantly share code, notes, and snippets.

@alexgreenbank
Created December 12, 2021 14:39
Show Gist options
  • Save alexgreenbank/6ca5a459f46a72f064d6b2bd3f4450a9 to your computer and use it in GitHub Desktop.
Save alexgreenbank/6ca5a459f46a72f064d6b2bd3f4450a9 to your computer and use it in GitHub Desktop.
#!/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