Verify my lasagne
Retina, 38 34 bytes
Thanks to Grimy for saving 4 bytes.
Have a regex with your lasagne.
Byte count assumes ISO 8859-1 encoding.
^([, ]+¶)?,{5,}(¶[@#]+¶[-~]*){2,}$
Assumes that the input ends with a trailing linefeed. Prints 1
(match) for valid lasagnes and 0
(no match) for invalid ones.
Try it online!
Explanation
This is just a standard .NET regex matched against the input, except that Retina provides the alias ¶
for linefeeds or \n
.
Since the input is guaranteed to be rectangular, we only need to check the width of the lasagne on one of the rows.
^ # Anchor the regex to the beginning of the input.
([, ]+¶)? # Match an optional first line of only commas an spaces.
,{5,} # Match at least 5 commas.
( # Match this at least twice to ensure at least two layers of sauce.
¶[@#]+ # Match a line of sauce.
¶[-~]* # Match a line of pasta. This line may be empty (which would
# indicate the end of the input.
){2,}
$ # Make sure we've indeed reached the end. Note that `$` can
# match either at the very end of the input, or in front of
# the trailing linefeed.
Grime, 43 bytes
e`[ \,]+/?/(\,/[#@]^/[\-~]/+/[#@]/?)+{5-,4-
Try it online!
Prints 1
for match and 0
for no match.
Explanation
Grime is designed for matching two-dimensional patterns, which are constructed piece by piece from smaller patterns. I define the optional top layer first, then the other layers by repeating a vertical stripe.
e` Match entire input against pattern:
/? Optionally
[ \,]+ a row of spaces and commas,
/ below that
( ) this pattern
+ repeated horizontally
{5-,4- having size at least 5x4.
The brace is closed implicitly.
"This pattern" is a vertical stripe containing
\, a comma,
/ below that
[#@]^/[\-~] a sauce character on top of a noodle character
(the ^/ is like / but with higher precedence)
/+ repeated vertically,
/ below that
/? optionally
[#@] a sauce character.