Befunge 98 - 26 31 20 19个字符
~:']-!\'[-!-+:0`j#q
摆脱了一些巨大的条件。现在该程序的工作方式如下:
~: read an input char and duplicate it
']-! push a 1 if the char is the ] char, 0 otherwise
\ swap the comparison value for the duplicated input char
'[-! push a 1 if the char is the [ char, 0 otherwise
- subtract the two comparison values. If the second one is 1, the first is 0,
so the result is -1. Else if the second one is 0, the first is 1 and the result is
1. Else, the first and the second are both 0 and the result is 0
+ Add the number to the counter (which is 0 if there is none already)
:0` test if the counter is positive (that'd mean an invalid program). Pushes a 1 if
positive, 0 otherwise.
j#q if the counter is positive, then this jumps to the q. Otherwise, it skips the q
and continues to the ~ at the beginning of the line. If there are no more chars in
the input, then the IP will be sent back to the q.
q
退出程序并弹出最高值作为错误值。误差值将是-1 1,如果有太多]
的0,如果他们是平衡的,并积极负,如果有太多[
的。如果号码是正负,那么很多这个数字的绝对值]
需要的平衡方案。
编辑:切换增量和减量。[
用于增加计数器并]
用于减少计数器。通过切换它,我节省了1个字符,因为对于退出条件,我只需要检查计数器是否为正,而不是负。
旧版本
~:'[-!j;\1+\#;']-!j;1-#;:0\`j#q
该代码的工作方式如下:
~ read one char of input
: duplicate it
'[-! push 1 if the character is [, 0 otherwise
j jump that number of characters
; skip until next ;
\1+\ increment counter
similarily for ].
#q when end of input is reached, ~ reflects the IP back. q ends the program with the error value on the stack.
编辑:意识到此接受的输入,例如][
,现在只要计数为负就结束,使用
:0\`j