createChallenges($numChallenges); $challenges = $this->createInterestingChallenges($numChallenges); $this->solveChallenges($challenges); /*echo "---- TESTS BELOW ----
\n"; $tests = array( "1000 2000 3000", // 4 "1111 1 10000", // 2 "9F 1F B7", // 23 "5 46 53", // 8 "56 8 65", // 9 "38 28 63", // 13 "2 1 3", // 4 "50 814 0864", // 9 "7 16 22" // 11 //"0 1 71799" // throw exception ); $this->solveChallenges($tests);*/ } public function solveChallenges($arr){ foreach($arr as $setStr){ $set = explode(" ",$setStr); $result = $this->determineBase($set[0],$set[1],$set[2]); if(!$result){ //echo "No solution. - ".$setStr."
\n"; }else{ echo "Valid Base: [".$result."] - Set:[".$setStr."]
\n"; $this->showMe($result,$setStr); } } } public function showMe($base, $setStr){ $set = explode(" ",$setStr); echo base_convert($set[0],$base,10)." + ".base_convert($set[1],$base,10)." = ".base_convert($set[2],$base,10)."
\n"; } public function createChallenges($numChallenges){ $challenges = array(); for($challengeIndex=0;$challengeIndex<$numChallenges;$challengeIndex++){ $a = $this->createRandStr(rand(1,20),$this->keyArr); $b = $this->createRandStr(rand(1,20),$this->keyArr); $c = $this->createRandStr(rand(1,20),$this->keyArr); //echo "Created: ".$a." ".$b." ".$c."
\n"; $challenges[$challengeIndex] = $a." ".$b." ".$c; } return $challenges; } public function createInterestingChallenges($numChallenges){ $challenges = array(); for($challengeIndex=0;$challengeIndex<$numChallenges;$challengeIndex++){ $a = $this->createNotSoRandStr(rand(1,4),0,4); $b = $this->createNotSoRandStr(rand(1,4),0,4); $c = $this->createNotSoRandStr(rand(1,4),0,4); //echo "Created: ".$a." ".$b." ".$c."
\n"; $challenges[$challengeIndex] = $a." ".$b." ".$c; } return $challenges; } public function createNotSoRandStr($splitMe,$ratioTop,$ratioBot){ $small = ceil($splitMe*$ratioTop/($ratioTop+$ratioBot)); $large = floor($splitMe*$ratioBot/($ratioTop+$ratioBot)); if($large < 1){ $large = 1; } $randNum = ""; if($small > 0){ $randNum = $this->createRandStr($small,$this->keyArr); } $randSubsetNum = $this->createRandStr($large,$this->keyArrSubset); $final = str_shuffle($randNum.$randSubsetNum); if(base_convert($final,32,10) < 1){ $final = $this->createNotSoRandStr($splitMe,$ratioTop,$ratioBot); } return $final; } public function createRandStr($num,$arr){ $randStr = ''; for($i=0;$i<$num;$i++){ $rand = rand(0 , count($arr)-1); $randStr .= $arr[$rand]; } if(base_convert($randStr,32,10) < 1){ $randStr = $this->createRandStr($num,$arr); } return $randStr; } public function determineBase($a,$b,$c){ if(!(is_string($a) && is_string($b) && is_string($c)) || !(base_convert($a,35,10) > 0 && base_convert($b,35,10) > 0 && base_convert($c,35,10) > 0) ){ throw new Exception("usage is determinBase(non-zero string,non-zero string,non-zero string):".$a." ".$b." ".$c); } if(strpbrk ($a.$b.$c,"XYZ") != ''){ //echo "Cause: Base over 32 - "; return false; } $lowestBase = $this->lowestPossibleBase($a,$b,$c); if(!$lowestBase){ //echo "Cause: Lowest base failure - "; return false; } //echo "Possible bases between:".$lowestBase." and 32
\n"; $valid = false; for($i=$lowestBase;$i<=32;$i++){ $baseTo10NumA = base_convert($a, $i, 10); $baseTo10NumB = base_convert($b, $i, 10); $baseTo10NumC = base_convert($c, $i, 10); //echo $i."
\n"; if($baseTo10NumA+$baseTo10NumB == $baseTo10NumC){ $valid = $i; break; } } if(!$valid){ //echo "Cause: Not valid for any base - "; } return $valid; } public function lowestPossibleBase($a,$b,$c){ // Abuse the fact that array indexes are base10 // And PHP SORT_REGULAR is awesome for mixed character values. $checkArr = array(); $ABCarr = $a.$b.$c; for($i=0;$ikeyArr); $checkArr[$this->keyArr[$key]] = $ABCarr[$i]; } rsort($checkArr,SORT_REGULAR); $key = array_search($checkArr[0], $this->keyArr); if($key > 0 && $key < 32){ return $key+1; } return false; } } // DONE $t = new Test(); $t->Start(1000);