diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-01 17:27:10 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-01 17:27:10 +0100 |
| commit | f04da024078df45ad80f095febfc8d4e158e9e08 (patch) | |
| tree | 49664911b0a09474897645b3042622c6c8941f54 | |
| parent | 0ace9393e4df7ad70d7eca6881febea3ac855ef1 (diff) | |
| download | perlweeklychallenge-club-f04da024078df45ad80f095febfc8d4e158e9e08.tar.gz perlweeklychallenge-club-f04da024078df45ad80f095febfc8d4e158e9e08.tar.bz2 perlweeklychallenge-club-f04da024078df45ad80f095febfc8d4e158e9e08.zip | |
- Tidied up solution.
| -rwxr-xr-x | challenge-076/mohammad-anwar/perl/ch-2.pl | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/challenge-076/mohammad-anwar/perl/ch-2.pl b/challenge-076/mohammad-anwar/perl/ch-2.pl index 94654f4c98..1133a7eddb 100755 --- a/challenge-076/mohammad-anwar/perl/ch-2.pl +++ b/challenge-076/mohammad-anwar/perl/ch-2.pl @@ -53,7 +53,7 @@ die "ERROR: Missing grid file.\n" unless defined $GRID; die "ERROR: Invalid word size [$SIZE].\n" unless (($SIZE =~ /^\d+$/) && ($SIZE > 0)); -my $words = search_words($GRID, $SIZE); +my $words = search_words($DICT, $GRID, $SIZE); if (keys %$words) { print sprintf("\nFound %d unique words:\n%s\n", scalar(keys %$words), @@ -69,16 +69,17 @@ else { # METHODS sub search_words { - my ($grid, $size) = @_; + my ($dict, $grid, $size) = @_; - my $words = (); + my $words = (); + my $dicts = fetch_dicts($dict, $size); my $puzzles = fetch_puzzles($grid, $size); if (scalar @$puzzles) { my $total = $#$puzzles + 1; my $index = 1; foreach my $puzzle (@$puzzles) { print sprintf("\rProcessing record %d of %d.", $index++, $total); - $words = match_word($DICT, $SIZE, $puzzle, $words); + $words = match_word($dicts, $puzzle, $words); } } @@ -86,14 +87,25 @@ sub search_words { } sub match_word { - my ($dictionary, $size, $puzzle, $words) = @_; + my ($dictionary, $puzzle, $words) = @_; + foreach my $word (keys %$dictionary) { + $words->{lc $word} = 1 if ($puzzle =~ /$word/i); + } + + return $words; +} + +sub fetch_dicts { + my ($dictionary, $size) = @_; + + my $words = (); open(my $WORDS, "<", $dictionary) or die "ERROR: Can't open $dictionary file: $!.\n"; while (my $word = <$WORDS>) { chomp $word; next unless (length($word) >= $size); - $words->{lc $word} = 1 if $puzzle =~ /$word/i; + $words->{lc $word} = 1; } close $WORDS; |
