+------------+
| |
| |
| |
+------+-----+
| | |
| | |
+------+-----+
+------------+
| |
| |
| |
+------------+
+------+
| |
| |
+------+
+-----+
| |
| |
+-----+
+---+
| |
+---+ |
| |
+-------+
<?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;
}