8
1
Smallfuck is a brainfuck-like language with 1-bit cells. It has the following instructions:
> Increment the pointer
< Decrement the pointer
* Flip the current bit
[ If the current bit is not set, jump to the instruction after the matching ]
] If the current bit is set, jump to the instruction after the matching [
( or just jump unconditionally to matching [ )
Whatfuck adds one more instruction:
? Nondeterministically set the current bit to 0 or 1.
A whatfuck program does not take any input. It can result in one of 3 possibilities: 1 (accept), 0 (reject) or it may never halt.
The program will result in 1 if there exists a sequence of bits chosen for ?s which results in the program terminating with 1 as the current bit.
The program terminates with 0 if all possible choices terminate with current bit 0,
If some choices don't terminate, and all choices which terminate do so with 0, then the program will never terminate.
Your interpreter should run all possibilities simultaneously. You can't try 0 first and then try 1, because some programs will not terminate when they should. For example, *[?*]* will accept with the choice 1, but never terminate if you always choose 0.
As an example, here is a python 2 interpreter I wrote, not golfed
Rules
Your interpreter must accept a whatfuck program from stdin and print its result.
You may assume the whatfuck program only contains characters
[]<>*?The array of bits is unbounded on both ends.
Shortest code wins.
Some Test Cases
This will fail if your code always tries 0 first
*[?*]*
1
Is there a subset of {-7,-3, 5, 8} whose sum is 3?
*<<<<<?[<<<<<<<<<<<<<<]?[<<<<<<]?[>>>>>>>>>>]?[>>>>>>>>>>>>>>>>]<
1
Is there a subset of {-7,-3, 5, 8} whose sum is 4?
*<<<<<<<?[<<<<<<<<<<<<<<]?[<<<<<<]?[>>>>>>>>>>]?[>>>>>>>>>>>>>>>>]<
0
Is there a way to assign boolean values to a,b and c such that
(a XOR b) AND (a XOR c) AND (b XOR c) is true?
?[*>*>*<<]?[*>*>>*<<<]?[*>>*>*<<<]>[*>[*>[*>*<]<]<]>>>
0
Save 1 byte by replacing
t+=(c=='>')-(c=='<');witht+=c=='>';t-=c=='<';, another by replacingB=B+[t]*(c=='*')withB+=[t]*(c=='*'), and a third by replacingp+=1+F.get(p,0)*(1-b)-R.get(p,0)*b;withp+=1+F.get(p,0)*-~-b-R.get(p,0)*b;. Great answer! (Sorry, I know this answer's super old!) – osuka_ – 2018-01-04T05:03:29.830