Last active
December 14, 2015 03:59
-
-
Save dav-m85/5025485 to your computer and use it in GitHub Desktop.
Work in progress, a php script to solve the trinomino line problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function swap($array, $a, $b) { | |
if( empty($array) || !is_array($array) ){ | |
return $array; | |
} | |
$j = count($array); | |
$ael = array($array[$a]); | |
$bel = array($array[$b]); | |
array_splice($array, $a, 1, $bel); | |
array_splice($array, $b, 1, $ael); | |
return $array; | |
} | |
class Triomino implements ArrayAccess{ | |
private $container = array(); | |
private $up = true; | |
public function offsetSet($offset, $value) { | |
if (is_null($offset)) { | |
$this->container[] = $value; | |
} else { | |
$this->container[$offset] = $value; | |
} | |
} | |
public function offsetExists($offset) { | |
return isset($this->container[$offset]); | |
} | |
public function offsetUnset($offset) { | |
unset($this->container[$offset]); | |
} | |
public function offsetGet($offset) { | |
return isset($this->container[$offset]) ? $this->container[$offset] : null; | |
} | |
function __construct(){ | |
$arr = func_get_args(); | |
if(count($arr) != 3) throw new Exception('oups'); | |
$this->container = $arr; | |
} | |
function flip(){ | |
$this->up = ! $this->up; | |
} | |
function isUp(){ | |
return $this->up; | |
} | |
function rotate($clockwise = true){ | |
if($clockwise){ | |
array_unshift($this->container, array_pop($this->container)); | |
} | |
else{ | |
array_push($this->container, array_shift($this->container)); | |
} | |
} | |
function draw(){ | |
if($this->up){ | |
$str = <<<EOF | |
/{$this->container[0]}\ | |
/{$this->container[2]} {$this->container[1]}\ | |
EOF; | |
} | |
else{ | |
$str = <<<EOF | |
\\{$this->container[0]} {$this->container[1]}/ | |
\\{$this->container[2]}/ | |
EOF; | |
} | |
return $str; | |
} | |
} | |
$a = new Triomino(1,2,3); | |
$b = new Triomino(2,2,1); | |
$l = new Line(); | |
$l->push($a); | |
$a->rotate(); | |
$l->push($b); | |
$l->flip(); | |
echo $l->draw(); | |
echo $l->checkTwo(0, 1) . PHP_EOL; | |
$b->rotate(); | |
echo $l->draw(); | |
echo $l->checkTwo(0, 1) . PHP_EOL; | |
class Line{ | |
private $trios = array(); | |
function push(Triomino $trio){ | |
$a = count($this->trios); | |
if($a != 0){ | |
$last = $this->trios[$a - 1]; | |
if($last->isUp()){ | |
$trio->flip(); | |
} | |
} | |
array_push($this->trios, $trio); | |
} | |
function flip(){ | |
foreach($this->trios as $tr){$tr->flip();} | |
} | |
function draw(){ | |
$l1 = $l2 = ''; | |
foreach($this->trios as $tr){ | |
$ar = explode(PHP_EOL, $tr->draw()); | |
$l1 .= $ar[0]; | |
$l2 .= $ar[1]; | |
} | |
return $l1 . PHP_EOL . $l2 . PHP_EOL; | |
} | |
function checkTwo($a, $b){ | |
$a = $this->trios[$a]; | |
$b = $this->trios[$b]; | |
if($a->isUp()){ | |
return ($a[0] == $b[0] && $a[1] == $b[2]); | |
} | |
else{ | |
return ($a[1] == $b[0] && $a[2] == $b[2]); | |
} | |
} | |
} | |
die; | |
// slightly adapted from http://stackoverflow.com/questions/3742506/php-array-combinations | |
class Combinations implements Iterator | |
{ | |
protected $c = null; | |
protected $s = null; | |
protected $n = 0; | |
protected $k = 0; | |
protected $pos = 0; | |
function __construct($s, $k = null) { | |
if(is_array($s)) { | |
$this->s = array_values($s); | |
$this->n = count($this->s); | |
} else { | |
$this->s = (string) $s; | |
$this->n = strlen($this->s); | |
} | |
if($k == null){ | |
$k = count($this->s); | |
} | |
$this->k = $k; | |
$this->rewind(); | |
} | |
function key() { | |
return $this->pos; | |
} | |
function current() { | |
$r = array(); | |
for($i = 0; $i < $this->k; $i++) | |
$r[] = $this->s[$this->c[$i]]; | |
return is_array($this->s) ? $r : implode('', $r); | |
} | |
function next() { | |
if($this->_next()) | |
$this->pos++; | |
else | |
$this->pos = -1; | |
} | |
function rewind() { | |
$this->c = range(0, $this->k); | |
$this->pos = 0; | |
} | |
function valid() { | |
return $this->pos >= 0; | |
} | |
// | |
protected function _next() { | |
$i = $this->k - 1; | |
while ($i >= 0 && $this->c[$i] == $this->n - $this->k + $i) | |
$i--; | |
if($i < 0) | |
return false; | |
$this->c[$i]++; | |
while($i++ < $this->k - 1) | |
$this->c[$i] = $this->c[$i - 1] + 1; | |
return true; | |
} | |
} | |
$trio = array(); | |
function cat1(){ | |
for($i = 0; $i <= 5; $i++){ | |
$res[] = array($i, $i, $i); | |
} | |
return array($res); | |
} | |
function cat2(){ | |
for($k = 0; $k <= 4; $k++){ | |
for($i = 0; $i <= 5; $i++){ | |
$j = ($i + 1 + $k) % 6; | |
$res[$k][] = array($i, $j, $j); | |
} | |
} | |
return $res; | |
} | |
function cat3(){ | |
for($k = 0; $k <= 2; $k++){ | |
for($i = 0; $i <= 5; $i++){ | |
$j = ($i + 1 + $k) % 6; | |
$l = ($i + 2 + $k) % 6; | |
$res[$k][] = array($i, $j, $l); | |
} | |
} | |
return $res; | |
} | |
function solve($trios){ | |
foreach(new Combinations(array(array(0, 1, 2), array(1, 0, 3), array(3,0,2)), 2) as $subset){ | |
show($subset); | |
} | |
} | |
// inspired from http://stackoverflow.com/questions/2920315/permutation-of-array | |
function permute($array, $k = 0){ | |
for($i = $k; $i < count($array); $i++){ | |
swap($array, $i, $k); | |
permute($array, $k+1); | |
swap($array, $k, $i); | |
} | |
if($k == count($array) - 1){ | |
var_dump($array); | |
} | |
} | |
// http://stackoverflow.com/questions/2920315/permutation-of-array | |
permute(array(1, 2, 3, 4)); | |
die; | |
function show($trio){ | |
foreach($trio as $tr){ | |
$str = join($tr, ' '); | |
print $str . PHP_EOL; | |
} | |
} | |
$c1 = cat1(); $c2 = cat2(); $c3 = cat3(); | |
$c = array_merge($c1, $c2, $c3); | |
foreach($c as $t){ | |
// show($t); | |
// echo PHP_EOL; | |
} | |
solve(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment