Skip to content

Instantly share code, notes, and snippets.

@Osukaru
Created July 26, 2013 10:09
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 Osukaru/6087781 to your computer and use it in GitHub Desktop.
Save Osukaru/6087781 to your computer and use it in GitHub Desktop.
Dragon simulator
<?php
$allFollowingPieces = array(
'a' => array('b', 'E'),
'b' => array('c', 'D'),
'c' => array('d', 'C'),
'd' => array('e', 'B'),
'e' => array('a', 'A'),
'A' => array('B', 'e'),
'B' => array('C', 'd'),
'C' => array('D', 'c'),
'D' => array('E', 'b'),
'E' => array('A', 'a'),
);
$pieces = array();
$steps = 25;
$pieces_number = $steps/5;
$count = 0;
for ($i=0; $i <= $pieces_number; $i++) {
$pieces['a'] = $pieces_number - $i;
$pieces['b'] = $pieces_number - $i;
$pieces['c'] = $pieces_number - $i;
$pieces['d'] = $pieces_number - $i;
$pieces['e'] = $pieces_number - $i;
$pieces['A'] = $i;
$pieces['B'] = $i;
$pieces['C'] = $i;
$pieces['D'] = $i;
$pieces['E'] = $i;
$lastPiece = 'e';
$solution = array();
$count = putPiece($lastPiece, $pieces, $allFollowingPieces, $solution, $count);
}
echo $count . PHP_EOL;
function putPiece($lastPiece, $pieces, $allFollowingPieces, $solution, $count) {
if (array_sum($pieces) == 0) {
//echo implode(', ', $solution).PHP_EOL;
$count++;
} else {
$followingPieces = $allFollowingPieces[$lastPiece];
foreach ($followingPieces as $followingPiece) {
if ($pieces[$followingPiece] > 0) {
$newPieces = $pieces;
$newPieces[$followingPiece] = $pieces[$followingPiece] - 1;
$newSolution = $solution;
$newSolution[] = $followingPiece;
$count = putPiece($followingPiece, $newPieces, $allFollowingPieces, $newSolution, $count);
}
}
}
return $count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment