Is it a valid hidden word?
The Universal Crossword has a set of guidelines for crossword puzzle submissions.
In this challenge we are going to be concerned with their rules for hidden word themes. A hidden word clue consists of a clue and a word. It can either be a "word break" or a "bookend".
For a word break the word must not appear as a contiguous substring of the clue, but if all the spaces are removed from the clue, then it is a contiguous substring with a non-empty prefix and suffix. Some examples:
-
POOR HOUSE,RHO: is valid. Solution:POOR HOUSE -
IMPROPER USE,PERUSE: is not valid. It appears separated by a space:IMPROPER USE, but the suffix is empty. -
SINGLE TRACK,SINGLET: is not valid. It appears separated by a space:SINGLE TRACK, but the prefix is empty. -
PLANE TICKET,ET: is not valid. The word appears separated by a space:PLANE TICKET, but it also appears contiguously:PLANE TICKET.
For a bookend the word must appear as a combination of a non-empty prefix and a non-empty suffix of the clue, but is not a contiguous substring. Bookends may span word breaks, but are not required to. The clue must not appear as a contiguous substring to be a valid bookend.
-
SINGLE TRACK,SICK: is valid. Solution:SINGLE TRACK -
YOU MUST DOT YOUR IS AND CROSS YOUR TS,YURTS: is valid. Solution:YOU MUST DOT YOUR IS AND CROSS YOUR TS -
STAND CLEAR,STAR: is valid, even though there are two solutions:STAND CLEARandSTAND CLEAR -
START A WAR,STAR: is not valid since the word is a prefix of the clue. -
TO ME,TOME: is valid. It can be split multiple ways including ways with empty prefixes and suffixes. -
TWO BIRDS WITH TWO STONE,TONE: is not valid since the word is a suffix of the clue -
IMPROPER USE,PERUSE: is not valid. It appears as a suffix and is not a contiguous substring:IMPROPER USE, but the prefix needs to be non empty for a bookend.
You will take as input a word (consisting of letters A-Z) and a clue (consisting of letters A-Z and spaces) and you must determine if the word is a valid solution to the clue by the above rules.
If the input is a valid pair you must output one consistent value, if it is not you must output a distinct consistent value.
This is code golf so the goal is to minimize the size of your source code as measured in bytes.
Test cases
Valid:
POOR HOUSE, RHO
SINGLE TRACK, SICK
YOU MUST DOT YOUR IS AND CROSS YOUR TS, YURTS
STAND CLEAR, STAR
TO ME, TOME
IN A PICKLE, NAP
Invalid:
IMPROPER USE, PERUSE
SINGLE TRACK, SINGLET
PLANE TICKET, ET
START A WAR, STAR
TWO BIRDS WITH ONE STONE, TONE
2 answers
Ruby, 128 bytes
->x,y{g=->c{c.chars.join" ?"}
!x[y]&&(x.match?(/.+#{g[y]}.+/)||(0...(y.size-1)).any?{x[/^#{g[y[0.._1]]}.+#{g[y[(_1+1)..]]}$/]})}
bookends are found with a constructed regex that takes most of the bytes.
Perl, 113 bytes
reads line from stdin formatted as the test cases. Exits with 1 valid or 0 if invalid.
perl -lF,. -E'($_,$n)=@F;@r=map{$n=~s/.{$_}\K/.*/r}1..length$n-1;$"="|";exit(!/$n/&&((y/ //d,/.$n./)||/^(@r)$/))'
perl -lF,. -E'
# load clue and word
($_,$n) = @F;
# create all permutations of prefix + ".*" + suffix of word
@r = map {
$n =~ s/.{$_}\K/.*/r
} 1..length$n-1;
# set array->string interpolation character for use in regex
$"="|";
exit(
!/$n/ && ( # word not in clue, and (
(y/ //d,/.$n./) || # is word break, or
/^(@r)$/ # is bookend )
)
)
'

1 comment thread