Created
June 20, 2011 16:16
-
-
Save xavierlacot/1035913 to your computer and use it in GitHub Desktop.
internet en moins de huit secondes
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 | |
// INTERNET EN MOINS DE HUIT SECONDES | |
class decoder | |
{ | |
protected | |
$code = array(), | |
$codeCombinations = array(), | |
$operations = array(), | |
$operationCombinations = array(); | |
public function __construct($code, $operations) | |
{ | |
$this->code = $code; | |
$this->operations = $operations; | |
} | |
protected function generateCombinations($begin, $code, $result = array()) | |
{ | |
if (count($code) > 0) | |
{ | |
foreach ($code as $i => $item) | |
{ | |
$sliced = $code; | |
array_splice($sliced, $i, 1); | |
$result = array_merge( | |
$result, | |
$this->generateCombinations(array_merge($begin, array($item)), $sliced) | |
); | |
} | |
} | |
else | |
{ | |
$key = implode('|', $begin); | |
$result[$key] = $begin; | |
} | |
return $result; | |
} | |
public function generate() | |
{ | |
$this->codeCombinations = $this->generateCombinations(array(), $this->code); | |
$this->codeCombinations = array_values($this->codeCombinations); | |
foreach ($this->operations as $letter => $operation) | |
{ | |
$this->operationCombinations[$letter] = array_values($this->generateCombinations(array(), $operation)); | |
} | |
} | |
protected function test($code, $combination) | |
{ | |
$operation = ''; | |
foreach ($code as $i => $number) | |
{ | |
$operation .= $number; | |
if (isset($combination[$i])) | |
{ | |
$operation .= ' '.$combination[$i].' '; | |
} | |
} | |
$operation = '$result = ('.$operation.');'; | |
eval($operation); | |
return (24 === $result); | |
} | |
public function testCombinations($operations = array()) | |
{ | |
$letters = array(); | |
foreach ($this->codeCombinations as $code) | |
{ | |
foreach ($this->operationCombinations as $letter => $combinations) | |
{ | |
foreach ($combinations as $combination) | |
{ | |
if ($this->test($code, $combination)) | |
{ | |
$letters[] = $letter; | |
} | |
} | |
} | |
} | |
$letters = array_unique($letters); | |
sort($letters); | |
var_dump(implode(' ', $letters)); | |
} | |
} | |
$codes = array( | |
array(6,6,6,9), | |
array(5,5,9,9), | |
array(4,5,10,11), | |
array(10,12,12,12), | |
array(1,1,13,13), | |
array(2,8,9,12), | |
array(4,4,7,13), | |
array(2,5,7,9), | |
array(3,5,12,12), | |
array(3,8,8,12), | |
array(8,8,12,13), | |
array(4,7,8,10), | |
array(4,5,10,12), | |
array(4,4,5,5), | |
array(1,1,2,11), | |
array(3,3,3,5), | |
array(2,3,5,6), | |
array(2,3,12,12), | |
array(11,12,13,13), | |
array(3,4,5,10), | |
array(5,9,10,11), | |
array(3,3,5,10), | |
array(2,5,8,8), | |
array(2,2,2,3), | |
array(2,5,6,9), | |
array(3,9,9,11), | |
array(2,2,5,7), | |
array(3,3,3,11), | |
array(2,5,7,7), | |
); | |
$operations = array( | |
'B' => array('*', '+', '-'), | |
'C' => array('*', '*', '*'), | |
'D' => array('*', '*', '+'), | |
'E' => array('*', '*', '-'), | |
'F' => array('+', '+', '+'), | |
'G' => array('+', '+', '-'), | |
'H' => array('*', '/', '/'), | |
'I' => array('*', '*', '/'), | |
'J' => array('/', '/', '+'), | |
'K' => array('-', '-', '-'), | |
'L' => array('/', '-', '-'), | |
'M' => array('/', '+', '-'), | |
'N' => array('*', '/', '-'), | |
'O' => array('*', '/', '+'), | |
'P' => array('/', '/', '-'), | |
'Q' => array('/', '/', '/'), | |
'R' => array('+', '-', '-'), | |
'S' => array('*', '+', '+'), | |
'T' => array('*', '-', '-'), | |
'U' => array('/', '+', '+') | |
); | |
foreach ($codes as $code) | |
{ | |
$decoder = new decoder($code, $operations); | |
$decoder->generate(); | |
$decoder->testCombinations($operations); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment