10
1
Prelude is an esoteric programming language, which has very few, but unusual, restrictions on what constitutes a valid program. Any block of printable ASCII text ("block" meaning that lines of printable ASCII are separated by newlines - 0x0A) is valid provided that:
- Every (vertical) column of text contains at most one of
(
and)
. - Ignoring their vertical position, the
(
and)
are balanced, that is, each(
is paired with exactly one)
to the right of it, and vice versa.
Write a program or function which, given a string containing printable ASCII and newlines, determines if it constitutes a valid Prelude program. You may take input via STDIN (or closest alternative), command-line argument or function argument. The result may be returned or printed to STDOUT, using any two fixed truthy/falsy values of your choice.
You must not assume that the input is rectangular.
This is code golf, so the shortest submission (in bytes) wins.
Examples
The following are valid Prelude programs (in fact, they're even real Prelude programs):
?1-(v #1)-
1 0v ^(# 0)(1+0)#)!
(#) ^#1-(0 #
1(# 1) v # - 1+)
vv (##^v^+
? v-(0 # ^ #)
?
1+ 1-!
And here are a number of inputs, all of which are invalid:
#(#(##)##)##(
)##(##(##)#)#
#(#)
)###
#(##
(##)
(##)
(#)#
(##)
(###
#(#)
(##)
#(#)
###)
#()#
()##
#(#)##
###
###(#)
Does Prelude have comments which could block out a close paren? – Alex A. – 2015-03-02T15:23:09.677
@Alex No. The above rules are really all there is to deciding whether a program is valid or not. – Martin Ender – 2015-03-02T15:24:35.147
Cool, thanks for clarifying. Just wanted to make sure. – Alex A. – 2015-03-02T15:25:22.980
Rule 1 - "Every column of text contains at most one of ( and )"; Example 1, line 2: "1 0v ^(# 0)(1+0)#)!" --> I see 3
)
and 2(
. Shouldn't it be only 1 per line? – Ismael Miguel – 2015-03-02T20:02:38.240@IsmaelMiguel you're looking at lines/rows, not columns. There may be only one
(
or)
per vertical column. – Martin Ender – 2015-03-02T20:03:29.243@MartinBüttner That isn't specified in the question. – Ismael Miguel – 2015-03-02T20:07:02.540
1
@IsmaelMiguel "column" is usually understood to refer to vertical lines (especially in the context of grids). I've clarified it anyway, though.
– Martin Ender – 2015-03-02T20:21:58.003Are
)(
and)()(
valid programs? – Blackhole – 2015-03-02T21:11:10.070@Blackhole No, those parentheses aren't correctly balanced. "... each
(
and be paired with exactly one)
to the right of it, and vice versa." – Martin Ender – 2015-03-02T21:16:37.173Okay, thanks for the clarification! – Blackhole – 2015-03-02T21:19:20.680
Can we assume the input has a newline at the end? – Omar – 2015-03-05T17:06:17.813
@Omar yes you can – Martin Ender – 2015-03-05T17:17:55.883