function boolfuck (code, input = "")
function boolfuck(string $code, string $input = ""): string {
// Implement your interpreter here
}
class InterpreterTest extends TestCase {
public function testOfficialHelloWorld() {
// Hello World Program taken from the official website
$this->assertEquals("Hello, world!\n", boolfuck(";;;+;+;;+;+;
+;+;+;+;;+;;+;
;;+;;+;+;;+;
;;+;;+;+;;+;
+;;;;+;+;;+;
;;+;;+;+;+;;
;;;;;+;+;;
+;;;+;+;;;+;
+;;;;+;+;;+;
;+;+;;+;;;+;
;;+;;+;+;;+;
;;+;+;;+;;+;
+;+;;;;+;+;;
;+;+;+;"), "Your interpreter did not work with the code example provided on the official website");
}
public function testMoreExamples() {
// Echo until byte(0) encountered
$this->assertEquals("Codewars", boolfuck(">,>,>,>,>,>,>,>,>+<<<<<<<<+[>+]<[<]>>>>>>>>>[+<<<<<<<<[>]+<[+<]>;>;>;>;>;>;>;>;>+<<<<<<<<+[>+]<[<]>>>>>>>>>[+<<<<<<<<[>]+<[+<]>>>>>>>>>+<<<<<<<<+[>+]<[<]>>>>>>>>>[+]+<<<<<<<<+[>+]<[<]>>>>>>>>>]<[+<]>,>,>,>,>,>,>,>,>+<<<<<<<<+[>+]<[<]>>>>>>>>>]<[+<]", "Codewars" . chr(0)));
// Two numbers multiplier
$this->assertEquals(chr(72), boolfuck(">,>,>,>,>,>,>,>,>>,>,>,>,>,>,>,>,<<<<<<<<+<<<<<<<<+[>+]<[<]>>>>>>>>>[+<<<<<<<<[>]+<[+<]>>>>>>>>>>>>>>>>>>+<<<<<<<<+[>+]<[<]>>>>>>>>>[+<<<<<<<<[>]+<[+<]>>>>>>>>>+<<<<<<<<+[>+]<[<]>>>>>>>>>[+]>[>]+<[+<]>>>>>>>>>[+]>[>]+<[+<]>>>>>>>>>[+]<<<<<<<<<<<<<<<<<<+<<<<<<<<+[>+]<[<]>>>>>>>>>]<[+<]>>>>>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<+[>+]<[<]>>>>>>>>>[+<<<<<<<<[>]+<[+<]>>>>>>>>>+<<<<<<<<+[>+]<[<]>>>>>>>>>[+]<<<<<<<<<<<<<<<<<<<<<<<<<<[>]+<[+<]>>>>>>>>>[+]>>>>>>>>>>>>>>>>>>+<<<<<<<<+[>+]<[<]>>>>>>>>>]<[+<]<<<<<<<<<<<<<<<<<<+<<<<<<<<+[>+]<[<]>>>>>>>>>[+]+<<<<<<<<+[>+]<[<]>>>>>>>>>]<[+<]>>>>>>>>>>>>>>>>>>>;>;>;>;>;>;>;>;<<<<<<<<", chr(8) . chr(9)));
}
}
function boolfuck(string $code, string $input = ""): string {
$i = 0;
$data = [];
$inputs = str_split($input);
$bitInput = '';
$bitInputs = [];
if ($input !== '') {
foreach ($inputs as $letter) {
$bitInput .= str_pad(strrev(decbin(ord($letter))) , 8, '0', STR_PAD_RIGHT);
}
$bitInputs = str_split($bitInput);
}
$ptr = 0;
$result = '';
do {
$instruction = $code[$i];
switch ($instruction) {
// > Moves the pointer right by 1 bit.
case '>':
++$ptr;
break;
// < Moves the pointer left by 1 bit.
case '<':
--$ptr;
break;
// + Flips the value of the bit under the pointer
case '+':
$data = initializeData($data, $ptr);
$data[$ptr] = (int) !$data[$ptr];
break;
// ; Outputs the bit under the pointer to the output stream.
// The bits get output in little-endian order, the same order in which they would be input.
// If the total number of bits output is not a multiple of eight at the end of the program,
// the last character of output gets padded with zeros on the more significant end.
case ';':
$data = initializeData($data, $ptr);
$result .= $data[$ptr];
break;
// , Reads a bit from the input stream, storing it under the pointer.
// The end-user types information using characters, though.
// Bytes are read in little-endian order—the first bit read from the character
case ',':
if (count($bitInputs) > 0) {
$dataToAdd = array_shift($bitInputs);
$data[$ptr] = $dataToAdd;
}
break;
// [ If the value under the pointer is 0 then skip to the corresponding ].
case '[':
$data = initializeData($data, $ptr);
if (0 === $data[$ptr]) {
$nbDoors = 0;
++$i;
do {
if ('[' === $code[$i]) {
++$nbDoors;
} elseif ($nbDoors > 0 && ']' === $code[$i]) {
--$nbDoors;
}
++$i;
} while (!(']' === $code[$i] && $nbDoors === 0));
}
break;
// ] Jumps back to the matching [ character.
case ']':
$data = initializeData($data, $ptr);
if (1 === $data[$ptr]) {
$nbDoors = 0;
--$i;
do {
if (']' === $code[$i]) {
++$nbDoors;
} elseif ($nbDoors > 0 && '[' === $code[$i]) {
--$nbDoors;
}
--$i;
} while (!('[' === $code[$i] && $nbDoors === 0));
}
break;
}
++$i;
} while (isset($code[$i]));
$decodedResult = '';
foreach (array_chunk(str_split($result), 8) as $resultChunk) {
$decodedResult .= chr(bindec(strrev(implode('', $resultChunk))));
}
return $decodedResult;
}
function initializeData($data, $ptr)
{
if (!isset($data[$ptr])) {
$data[$ptr] = 0;
}
return $data;
}