QUILT - Little Quilt

Little Quilt is a small language introduced by Ravi Sethi in his book ‘Programming Languages’.

Here, a restricted version of Little Quilt is presented. The language is defined by the following BNF grammar:

    < QUILT > ::= A | B | turn(< QUILT >) | sew(< QUILT >,< QUILT >)

A and B represent the two primitive quilts. Each primitive quilt corresponds to a matricial arrangement of 2 × 2 characters. turn() and sew() are operations over quilts.

The instruction turn(x) turns the quilt x 90 degrees clockwise. The following table illustrates the primitive quilts as well as examples of the effect of the turn() operation:

Quilt Operations

Accordingly, the instruction sew(x,y) sews quilt x to the left of quilt y. Both x and y must have the same height, otherwise an error will be generated. The following figure represents the result of sew(A,turn(B)):

Quilt

while the sew(turn(sew(B,turn(B))),A) generates an error message.

Your job is to build an interpreter of the Little Quilt language.

Input

The input file will be a text file containing different Little Quilt expressions, each one ended by a semicolon character (;). Space and new line characters must be ignored; this means that an expression may span several lines.

Output

The output file contains the quilts produced as a result of interpreting the input expressions.

Each quilt must be preceded by a line, left aligned, with the format

Quilt i:
where i is the quilt number, starting at 1. If the expression interpretation generates and error, the word error must be printed.

Example

Input:
sew(turn(sew(B,turn(B))),
turn(sew(turn(B),B))) ;


sew(turn(sew(B,turn(B))),A); sew(turn(sew(A,turn(A))), turn(turn(
turn(sew(A,turn(A)))))) ; Output: Quilt 1: ||-- ||-- --|| --|| Quilt 2: error Quilt 3: \\// +\/+ +/\+ //\\

Added by:Camilo Andrés Varela León
Date:2006-10-25
Time limit:0.200s-0.400s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:XX Colombian National Programming ACM 2006

hide comments
2020-01-03 00:46:15
Fun little problem. My 4-function solution with header, comments and safe IO handling is 69 lines in Python and was anything but tedious to write. But yeah, if you choose C to solve stuff like this you might as well type with hammers.

Input is valid, aside from being purposefully messy that is. Maximum dimension of the final graph won't exceed 100.
2019-12-31 03:17:35 Erick Wong
Can be done in < 100 lines of code if you structure it well. This is one of the few contest problems where I found it beneficial to use pointers and a custom object. One of my personal faves.
2016-11-05 02:38:28
What is the data's range? What size will the final graph be ?
2013-12-03 04:27:24 Benito
Hi I need the solve of this problem :( I dont have time for solve, its for school, please send me the code in Java, send me to produccionesbenito@hotmail.com
2011-06-27 03:55:38 Santiago Palacio
Can we assume the input is valid? i mean, there will be no expressions like sew(a); ?
2009-10-10 02:43:19 abhijith reddy d
A Tedious one !!
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.