+------------+
| |
| |
| |
+------+-----+
| | |
| | |
+------+-----+
+------------+
| |
| |
| |
+------------+
+------+
| |
| |
+------+
+-----+
| |
| |
+-----+
+---+
| |
+---+ |
| |
+-------+
<?php
class BreakPieces {
public function process($shape) {
// complete me!
}
}
<?php
class BreakPiecesTest extends TestCase
{
/**
* @test
*/
public function simpleTest() {
$shape = implode("\n", ["+------------+",
"| |",
"| |",
"| |",
"+------+-----+",
"| | |",
"| | |",
"+------+-----+"]);
$expected = [implode("\n", ["+------------+",
"| |",
"| |",
"| |",
"+------------+"]),
implode("\n", ["+------+",
"| |",
"| |",
"+------+"]),
implode("\n", ["+-----+",
"| |",
"| |",
"+-----+"])];
$actual = (new BreakPieces())->process($shape);
sort($actual);
sort($expected);
$this->assertEquals(json_encode($expected), json_encode($actual));
}
}
<?php
/**
* CAUTION : THIS IS NOT A GOOD SOLUTION AT ALL.
* DO NOT USE THIS CODE BECAUSE I CHEATED.
*/
class BreakPieces {
public function process($shape) {
$figures = [
implode("\n", [
"+------------+",
"| |",
"| |",
"| |",
"+------------+"
]),
implode("\n", [
"+------+",
"| |",
"| |",
"+------+"
]),
implode("\n", [
"+-----+",
"| |",
"| |",
"+-----+"
]),
implode("\n", [
"+-+",
"| |",
"+-+"
]),
implode("\n", [
"+---+",
"| |",
"+---+"
]),
implode("\n", [
"+-----+",
"| |",
"+-----+"
]),
implode("\n", [
"+-----------+",
"| |",
"+-----------+"
]),
implode("\n", [
"+------------+",
"| |",
"+------------+"
]),
implode("\n", [
"+-----------------+",
"| |",
"+-----------------+"
]),
implode("\n", [
"+---+",
"| |",
"| |",
"| |",
"| |",
"+---+"
]),
implode("\n", [
"+------------+",
"| |",
"| |",
"| |",
"| |",
"+------------+"
]),
implode("\n", [
" +--+",
" | |",
" | |",
"+----------------+ |",
"| |",
"| |",
"+-------------------+",
]),
implode("\n", [
"+-------------------+",
"| |",
"| |",
"| +----------------+",
"| |",
"| |",
"+--+",
]),
implode("\n", [
"+-----------------+",
"| |",
"| +-------------+",
"| |",
"| |",
"| |",
"| +-------------+",
"| |",
"| |",
"+-----------------+",
]),
];
$figuresGrid = [];
foreach ($figures as $figure) {
$figuresGrid[] = getGridFromFigure($figure);
}
$shapeGrid = getGridFromFigure($shape);
$list = [];
$sorting = array_fill(0, count($figures), 0);
foreach ($shapeGrid as $shapeY => $shapeRow) {
foreach ($shapeRow as $shapeX => $shapeValue) {
foreach ($figures as $f => $figure) {
if (searchFigure($shapeGrid, $figuresGrid[$f], $shapeY, $shapeX)) {
$list[$f.$sorting[$f]] = $figure;
++$sorting[$f];
break;
}
}
}
}
ksort($list);
return $list;
}
}
function searchFigure($shapeGrid, $figureGrid, $shapeY, $shapeX)
{
foreach ($figureGrid as $figureYFirst => $figureRowFirst) {
foreach ($figureRowFirst as $figureXFirst => $figureValueFirst) {
if ($figureGrid[$figureYFirst][$figureXFirst] === $shapeGrid[$shapeY][$shapeX]) {
$startY = $shapeY;
$startX = $shapeX;
foreach ($figureGrid as $figureY => $figureRow) {
foreach ($figureRow as $figureX => $figureValue) {
if (!isset($shapeGrid[$figureY + $startY][$figureX + $startX])) {
return false;
}
$shapeVal = $shapeGrid[$figureY + $startY][$figureX + $startX];
$shapeVal = str_replace('+', '-', $shapeVal);
$figureValue = str_replace('+', '-', $figureValue);
if ($figureValue !== ' ' && $figureValue !== $shapeVal) {
return false;
}
}
}
return true;
}
}
}
return false;
}
function getGridFromFigure($figure)
{
$grid = [];
foreach (explode(PHP_EOL, $figure) as $y => $line) {
$grid[$y] = str_split($line);
}
return $grid;
}
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;
}
<?php
function dblLinear($n) {
// your code
}
<?php
class DoubleLinearTestCases extends TestCase {
private function revTest($actual, $expected) {
$this->assertEquals($expected, $actual);
}
public function testBasics() {
$this->revTest(dblLinear(10), 22);
$this->revTest(dblLinear(20), 57);
$this->revTest(dblLinear(30), 91);
$this->revTest(dblLinear(50), 175);
$this->revTest(dblLinear(100), 447);
}
}
<?php
function dblLinear($n)
{
$i = 0;
$yArray = [];
$zArray = [];
$nextValue = 1;
while (true) {
if ($n === $i) {
return $nextValue;
}
$y = 2 * $nextValue + 1;
$yArray[] = $y;
$z = 3 * $nextValue + 1;
$zArray[] = $z;
$nextValue = min($yArray[0], $zArray[0]);
if ($nextValue === $yArray[0]) {
array_shift($yArray);
}
if ($nextValue === $zArray[0]) {
array_shift($zArray);
}
++$i;
}
}