Skip to content

Instantly share code, notes, and snippets.

@ounziw
Created January 30, 2016 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ounziw/7348fc9022116d3b5b4c to your computer and use it in GitHub Desktop.
Save ounziw/7348fc9022116d3b5b4c to your computer and use it in GitHub Desktop.
nagoyaphp第10回 (2016年1月30日) の課題
<?php
/**
* 問題の説明は
* http://nabetani.sakura.ne.jp/hena/ord7selectchair/
*
* 座席:両端に、仮想上の席を用意する
*
* 座席に点数をつける
* 両隣空き : 0
* 方側空き : 1
* 両側埋まる : 2
* 誰かが座っている : 大きな数字 (2 より大きければOK)
* 仮想上の席 : 大きな数字 (2 より大きければOK)
*
* 大きな数字がある席は着席不可とすることで、「誰かが座っている席」「両端のさらに外」を除外する
*
*/
class Sheet {
protected $sheetvalue; // 座席の点数
protected $sheetpeople; // 座席にいる人。アルファベットor「-」
protected $inputdata; // 入力データのうち、人の情報を格納する
/**
* コンストラクタ
* 入力の分割
* 初期データの設定
*/
public function __construct($input) {
$inputarray = explode(':',$input);
$this->sheetvalue[] = strlen($input)+10;
$this->sheetpeople[] = '';
for($i=0;$i<$inputarray[0];$i++) {
$this->sheetvalue[] =0;
$this->sheetpeople[] = '-';
}
$this->sheetvalue[] = strlen($input)+10;
$this->sheetpeople[] = '';
$this->inputdata = str_split($inputarray[1]);
}
/**
* メインの処理
*/
public function process(){
foreach($this->inputdata as $val) {
if (ctype_upper($val)) {
$this->add($val);
} elseif(ctype_lower($val)) {
$this->remove($val);
} else {
echo 'Input Error!';
}
}
}
/**
* 座席の点数を調べる
*/
public function shortest($array) {
$shortest = min($array);
return array_search($shortest, $array);
}
/**
* 人が来るときの処理
*
* 点数の低い座席を探し、そこに座る
* 座った座席とその前後の座席の、点数を変更する
*/
protected function add($val) {
$pos = $this->shortest($this->sheetvalue);
$this->sheetpeople[$pos] = $val;
$this->sheetvalue[$pos-1] += 1;
$this->sheetvalue[$pos] += 10;
$this->sheetvalue[$pos+1] += 1;
}
/**
* 人が去るときの処理
*
* 座っていた座席の点数を再評価
* 前後の座席は -1
*/
protected function remove($val) {
$pos = array_search(strtoupper($val), $this->sheetpeople);
$this->sheetpeople[$pos] = '-';
$this->sheetvalue[$pos] -= 10;
$this->sheetvalue[$pos-1] -= 1;
$this->sheetvalue[$pos+1] -= 1;
}
/**
* データを出力する
*/
public function output() {
echo implode('',$this->sheetpeople);
}
/**
* デバッグ用
*/
function debug() {
var_dump($this->sheetpeople);
var_dump($this->sheetvalue);
var_dump($this->inputdata);
}
}
$input = '10:ABCDEFGHIJ';
//$input = '4:ABCbBD';
$obj = new Sheet($input);
$obj->process();
$obj->output();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment