Verify my lasagne

15

1

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.

Roman Gräf

Posted 2016-12-14T10:57:55.233

Reputation: 2 915

13Please close that parenthesis. – Quentin – 2016-12-14T16:40:34.123

Question: Does it need to be horizontally rectangular? i.e. what if it is 10 rows high and 9 columns wide? – Ruslan – 2016-12-14T17:31:08.933

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. – feersum – 2016-12-14T19:06:46.763

@feersum "Padded with spaces if necessary" – UKMonkey – 2016-12-15T11:42:04.293

Answers

11

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.

Martin Ender

Posted 2016-12-14T10:57:55.233

Reputation: 184 808

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. – Grimmy – 2016-12-14T14:17:11.327

@Grimy Ah good catch, thank you! – Martin Ender – 2016-12-14T14:20:45.980

5

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.

Zgarb

Posted 2016-12-14T10:57:55.233

Reputation: 39 083