phizel has asked for the wisdom of the Perl Monks concerning the following question:
Edit: So I finally got this particular test to work by iterating through the different directions in the reverse order plus only allowing one match per word. That feels a bit arbitrary, so still open to alternative algorithm suggestions.
The hidden message is supposed to be LYNNBUETTNERART, but my version produces LYNNBUETTNRART... (note the missing second E), whereas the the online tool gets this right. Can somebody help find the error in the algorithm or the implementation, or suggest a better algorithm?#!/usr/bin/env perl use 5.012; use warnings; use List::AllUtils qw(nsort_by reduce); my $grid = join "\n", qw( ONEFISHTWOJELLYFISHS LYNTAOBDOPAEPNBUETYE PMIRHSAESTNESRRARTEU CALIFORNIAPLEASANTLL HHSREDPOPPIESXVHUWLB AAAZECROFEFILERDLOAE PMLMPHSIFDEPIRTSYOVM PDLEQMIEWBLACKCATDSI YEADENRESURGENCEPYNT FNCIDOLBABYOCTOPUSOR AHTCYLMAERDSRELLEFTE MODEERFOTTHGILFWDPLM IUBRNEDRAGEMOHELHKAM LSSCOUNTRYWALKDTVQWU YEDNOPNEZORFPUSFRUSS ); my @words = qw( BABYOCTOPUS BLACKCAT CALIFORNIAPLEASANT CALLAS COUNTRYWALK EEL FELLERSDREAM FLIGHTTOFREEDOM FROZENPOND HAMDENHOUSE HAPPYFAMILY HOMEGARDEN LIFEFORCE ONEFISHTWOJELLYFISH PEAPODBOAT REDPOPPIES REEFDWELLERS RESURGENCE SEASHRIMP STRIPEDFISH SUMMERTIMEBLUES SURFSUP VASE WALTONSVALLEY WOODY ); # Look for the largest words first. @words = nsort_by { -length } @words; my $width = index $grid, "\n"; my @dir = map { [$_, 0, $grid], [$_, 1, $grid] } (0, $width-1 .. $widt +h+1); for my $word (@words) { for my $dir (@dir) { my @char = split '', $dir->[1] ? reverse $word : $word; my $re = join ".{$dir->[0]}", map "($_)", @char; if (my $i = $dir->[2] =~ /$re/s ) { substr $dir->[2], $-[$i++], 1, ' ' for @char; } } } my $message = reduce { $a & $b } map { $_->[2] } @dir; $message =~ tr/A-Z//cd; say $message;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Improving this word search solver
by Anonymous Monk on Jan 21, 2015 at 01:31 UTC | |
by phizel (Acolyte) on Jan 21, 2015 at 01:41 UTC | |
by Anonymous Monk on Jan 21, 2015 at 02:03 UTC | |
by phizel (Acolyte) on Jan 21, 2015 at 02:13 UTC | |
by Laurent_R (Canon) on Jan 21, 2015 at 07:24 UTC |