CodeIQのあみだの問題をやったのでメモ

https://codeiq.jp/q/1597

```lang:anida.php
<?php

/**
 * あみだマップクラス
 *
 * あみだ自身のマップ(配列)を保持し
 * 自身に関する動作の責任を持つクラス
 */
class AmidaMap
{
    private $dataMap = array();
    private $currentX = 0;
    private $currentY = 0;
    private $width = 0;
    private $height = 0;

    public function __construct($stdins)
    {
        $this->init($stdins);
    }

    protected function init($stdins)
    {
        foreach ($stdins as $std) {
            $tmps = explode(',', $std);
            $tmps[] = '0';
            $this->width = count($tmps);
            $this->dataMap[] = $tmps;
        }
        $this->height = count($this->dataMap);
    }

    public function clear($x, $y)
    {
        $this->currentX = $x;
        $this->currentY = $y;
    }

    public function getAmidaSize()
    {
        return $this->width;
    }

    public function isScopeRange()
    {
        if ($this->width > 50 || $this->height > 50) {
            return false;
        }

        return true;
    }

    protected function get($x = null, $y = null) {
        if (is_null($x) && is_null($y)) {
            if (! isset($this->dataMap[$this->currentY][$this->currentX])) {
                return 0;
            }
            return $this->dataMap[$this->currentY][$this->currentX];
        } else if (! is_null($x) && is_null($y)) {
            if (! isset($this->dataMap[$this->currentY][$x])) {
                return 0;
            }
            return $this->dataMap[$this->currentY][$x];
        } else if (is_null($x) && ! is_null($y)) {
            if (! isset($this->dataMap[$y][$this->currentX])) {
                return 0;
            }
            return $this->dataMap[$y][$this->currentX];
        } else {
            if (! isset($this->dataMap[$y][$x])) {
                return 0;
            }
            return $this->dataMap[$y][$x];
        }
    }

    public function slide()
    {
        if ($this->get()) {
            $this->slideRight();
        } else if ($this->get($this->currentX - 1)) {
            $this->slideLeft();
        }
    }

    protected function slideLeft()
    {
        $this->currentX--;
    }

    protected function slideRight()
    {
        $this->currentX++;
    }

    public function next()
    {
        if ($this->currentY + 1 > $this->height - 1) {
            return false;
        }
        $this->currentY++;
        return true;
    }

    public function getEndPoint()
    {
        return $this->currentX;
    }
}

/**
 * あみだセンタークラス
 *
 * あみだマップをコントロールする責任を持つクラス
 */
class AmidaCenter
{
    private $map;
    private $current = 0;

    public function __construct(AmidaMap $map)
    {
        $this->map = $map;
    }

    public function run()
    {
        if (! $this->map->isScopeRange()) {
            echo 'range error';
            exit;
        }
        $result = array();
        $amidaWidthMax = $this->map->getAmidaSize();
        for ($i = 0; $i < $amidaWidthMax; $i++) {
            do {
                $this->map->slide();
            } while ($this->map->next());
            $result[] = $this->map->getEndPoint() + 1;
            $this->map->clear($i + 1, 0);
        }

        echo implode(',', $result);
    }
}

/*
 * 標準入出力
 */
$stdins = array();
while(true)
{
    $stdin = trim(fgets(STDIN));
    if ($stdin === '')
    {
        break;
    }
    $stdins[] = $stdin;
}

$map = new AmidaMap($stdins);
$center = new AmidaCenter($map);
// あみだの実行
$center->run();
```