15
\$\begingroup\$

Scenario

I often cook lasagne, but I also have a few mistakes. Since I repeat these faults so often, I thought I maybe could do a program that checks if I did everything right.

Valid lasagne

A valid lasagne is

  • At least 5 columns wide
  • At least 4 layers high
    • Excluding extra cheese
  • The top layer is cheese (represented by ,)
  • The second layer is sauce (represented by either @ or #)
  • After that the layers alternate (1 layer sauce, 1 layer noodles (represented by either ~ or -))
  • Each column can have either one or two layers of cheese

The program

Should

  • take a lasagne string as input
    • multiline string
    • string array
    • the string only contains the chars ,@#~-
    • Rectangular
    • Padded with spaces if necessary
  • Output whether it's a valid lasagne
    • Any thing that's thruthy in your language if valid
    • Nothing or anything that's falsy in your language
  • be either
    • a full program
    • a function
    • that only uses functionality that was implemented before the 14.12.2016

Test cases

,, ,
,,,,,,
@@@###
~~~~~-
@##@@#

--> truthy


@@@#
----
@@##
----
@###

--> falsy (cause of cheese and width (You don't have to print the stuff in the brackets))


,,,,,
-----
@####
-----
@@@@@

--> falsy (have the sauce as last layer)

Winning criteria

The

submission wins.

\$\endgroup\$
  • 13
    \$\begingroup\$ Please close that parenthesis. \$\endgroup\$ – Quentin Dec 14 '16 at 16:40
  • \$\begingroup\$ Question: Does it need to be horizontally rectangular? i.e. what if it is 10 rows high and 9 columns wide? \$\endgroup\$ – Ruslan Dec 14 '16 at 17:31
  • \$\begingroup\$ The spec says it can only ,@#~- with the exception of spaces as padding, but the first test case contains spaces in the middle of a line. \$\endgroup\$ – feersum Dec 14 '16 at 19:06
  • \$\begingroup\$ @feersum "Padded with spaces if necessary" \$\endgroup\$ – UKMonkey Dec 15 '16 at 11:42
11
\$\begingroup\$

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.
\$\endgroup\$
  • \$\begingroup\$ If we’re allowed to assume that there’s a final linefeed after the last line (which sounds reasonable), you can use ¶[-~]* instead of (¶[-~]+|$), saving 4 bytes. \$\endgroup\$ – Grimy Dec 14 '16 at 14:17
  • \$\begingroup\$ @Grimy Ah good catch, thank you! \$\endgroup\$ – Martin Ender Dec 14 '16 at 14:20
5
\$\begingroup\$

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.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.