From 0c04fc8ce603860f5faceb1764f5b379b93697ce Mon Sep 17 00:00:00 2001 From: "Gustavo L. de M. Chaves" Date: Wed, 8 May 2019 22:34:00 -0300 Subject: Optimize a hash into an array --- challenge-007/gustavo-chaves/perl5/ch-2.pl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/challenge-007/gustavo-chaves/perl5/ch-2.pl b/challenge-007/gustavo-chaves/perl5/ch-2.pl index ddb900251f..5ced16a2d6 100755 --- a/challenge-007/gustavo-chaves/perl5/ch-2.pl +++ b/challenge-007/gustavo-chaves/perl5/ch-2.pl @@ -30,8 +30,8 @@ sub first_shortest_ladder { $distance += 1 if substr($word_i, $k, 1) ne substr($word_j, $k, 1); } if ($distance == 1) { - $graph{$word_i}{$word_j} = undef; - $graph{$word_j}{$word_i} = undef; + push @{$graph{$word_i}}, $word_j; + push @{$graph{$word_j}}, $word_i; } } $dist{$word_i} = @$wordlist + 1; # infinity @@ -47,7 +47,7 @@ sub first_shortest_ladder { last if $u eq $target; # found a shortest path to $target - foreach my $v (grep {exists $graph{$_}} keys %$neighbors) { + foreach my $v (grep {exists $graph{$_}} @$neighbors) { my $alt = $dist{$u} + 1; if ($alt < $dist{$v}) { $dist{$v} = $alt; -- cgit