From 0aae50e6dc686fad8e6d51097768f97cf60638f0 Mon Sep 17 00:00:00 2001 From: "Gustavo L. de M. Chaves" Date: Wed, 8 May 2019 22:37:04 -0300 Subject: Optimize the calculation of a constant --- challenge-007/gustavo-chaves/perl5/ch-2.pl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/challenge-007/gustavo-chaves/perl5/ch-2.pl b/challenge-007/gustavo-chaves/perl5/ch-2.pl index 5ced16a2d6..e9d757481a 100755 --- a/challenge-007/gustavo-chaves/perl5/ch-2.pl +++ b/challenge-007/gustavo-chaves/perl5/ch-2.pl @@ -21,7 +21,9 @@ sub first_shortest_ladder { my (%graph, %dist, %prev); - my $length = length $source; + my $length = length $source; + my $infinity = @$wordlist + 1; + for my $i (0 .. @$wordlist-2) { my $word_i = $wordlist->[$i]; foreach my $word_j (@{$wordlist}[$i+1 .. @$wordlist-1]) { @@ -34,7 +36,7 @@ sub first_shortest_ladder { push @{$graph{$word_j}}, $word_i; } } - $dist{$word_i} = @$wordlist + 1; # infinity + $dist{$word_i} = $infinity; $prev{$word_i} = undef; } -- cgit