- var_dump: documentation ( source)
- str_split: documentation ( source)
<?php
function compareCombinations($toFind, $toTry){
$nb = 4;
$goodPlace = 0;
$wrongPlace = 0;
$toFind = str_split($toFind);
$toTry = str_split($toTry);
var_dump($toFind, $toTry);
//Look for good places
for($i = 0; $i < $nb; $i++){
if($toFind[$i] === $toTry[$i]){
$goodPlace++;
}
}
//Look for wrong places
for($i = 0; $i < $nb; $i++){
for($j = 0; $j < $nb; $j++){
if($i == $j){
continue;
}
if($toTry[$i] === $toFind[$j]){
$wrongPlace++;
}
}
}
return array(
'good' => $goodPlace,
'wrong' => $wrongPlace,
'absent' => $nb - $goodPlace - $wrongPlace
);
}
foreach(array('0000' => '0001') as $find => $try){
$r = compareCombinations($find,$try);
var_dump(array(
'find' => $find,
'try' => $try
) + $r);
}