Created
April 3, 2010 02:38
-
-
Save arodland/354023 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$\="\n"; # "print for ..." at the end puts stuff on new lines :) | |
$/=""; # paragraph mode. | |
@word = split//,pop; # Get the search word off of the commandline | |
($board=<>) =~ tr/ //d; | |
$width = 1 + index($board, "\n"); # How wide is the board? Find the first newline. | |
for (1,2) { | |
# Find the word forwards, southwest, south, and southeast. | |
for my $stride (0, $width - 2 .. $width) { | |
$pattern = join "."x $stride, @word; | |
# If it's in the board, record the start and end match positions. | |
push @matches, [ $-[0], $+[0] - 1] while $board =~ /$pattern/sg; | |
} | |
# Reverse the word, so that on the next iter we find the word backwards, | |
# northeast, north, and northwest. | |
@word = reverse @word; | |
# And reverse the starts and ends. The forward matches will be reversed | |
# twice (and come out forwards); the backward ones will only be reversed | |
# once. | |
@matches = map [ reverse @$_ ], @matches; | |
} | |
sub pos_to_row_col { | |
my $pos = shift; | |
return sprintf "row %d, column %d", | |
(1 + $pos / $width), | |
(1 + $pos % $width); | |
} | |
for my $match (@matches) { | |
print pos_to_row_col( $match->[0] ) . " --> " | |
. pos_to_row_col( $match->[1] ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment