Skip to content

Instantly share code, notes, and snippets.

@caiyili
Last active January 3, 2016 15:59
Show Gist options
  • Save caiyili/8486540 to your computer and use it in GitHub Desktop.
Save caiyili/8486540 to your computer and use it in GitHub Desktop.
计算微博上一个题目的脚本!
<?php
$allSortFile = "allSort.txt" ;
if (!file_exists($allSortFile)){
$fout = fopen($allSortFile,"w");
$allSolution = allSort(1,10);
foreach($allSolution as $item){
$line = implode("\t",$item) ;
fputs($fout,$line . "\n") ;
}
fclose($fout) ;
}
$fin = fopen($allSortFile,"r") ;
$i = 0;
while(!feof($fin)) {
$i ++ ;
$line = trim(fgets($fin));
if (empty($line)) continue;
$keys = array('_1','_2','_3','_4','_5','_6','_7','_8','_9','_10') ;
$arrLine = explode("\t",$line);
$arr = array_combine($keys,$arrLine);
if (checkSolution($arr)) {
print_r($arr) ;
}
if ($i % 10000 == 0){
echo "try {$i} times ...\n" ;
}
}
function allSort($start,$end){
if ($start > $end){
throw new Exception("error") ;
}
elseif ($start == $end){
$rt = array();
for ($j = 'A'; $j <= 'D'; $j ++){
$rt[] = array("_$start"=>$j) ;
}
return $rt ;
}
else {
$first = $start ;
$midRes = array();
$solution = array();
for ($j = 'A'; $j <= 'D'; $j ++){
$midRes["_$first"] = $j ;
$remain = allSort($first+1,$end);
foreach ($remain as $one){
$midRes = array_merge($midRes,$one);
$solution[] = $midRes ;
}
}
return $solution ;
}
}
function checkSolution(array $arr) {
//1
$opt1 = array('A'=>'_1','B'=>'_2','C'=>'_3','D'=>'_4');
$firstA = array_search('A',$arr);
if ($firstA != $opt1[$arr['_1']]) {
return false ;
}
//2
$opt2 = array(
'A'=>5,
'B'=>6,
'C'=>7,
'D'=>8,
);
$find = false ;
for($i=1;$i<=9;$i++){
$j = $i + 1;
if ($arr["_$i"] == $arr["_$j"]){
$find = true ;
break ;
}
}
if (!$find || $i != $opt2[$arr["_2"]] ){
return false ;
}
//3
$opt3 = array(
'A'=>4,'B'=>9,'C'=>8,'D'=>2,
);
$k = "_" . $opt3[$arr["_3"]] ;
if ($arr["_3"] != $arr[$k]){
return false ;
}
//4
$opt4 = array(
'A'=>5,'B'=>4,'C'=>3,'D'=>2,
);
$totalA = count(array_filter($arr,function($v){
return 'A' == $v ;
}));
if ($totalA != $opt4[$arr["_4"]]) {
return false ;
}
//5
$opt5 = array(
'A'=>1,'B'=>2,'C'=>3,'D'=>4,
);
if ($arr["_5"] != $arr['_'.$opt5[$arr["_5"]]]) {
return false;
}
//6
$arrStat = array();
for ($j = 'A'; $j <= 'D'; $j ++){
$arrStat[$j] = count(array_filter($arr,function($v)use($j){
return ($j == $v) ;
}));
}
$find = false;
if ('B' == $arr["_6"] || 'C' == $arr["_6"]) {
if ($arrStat['C'] == $arrStat['A']) {
$find = true ;
}
} elseif('D'== $arr["_6"] ) {
if ($arrStat['A'] == $arrStat['D']){
$find = true ;
}
} else {
if ($arrStat['A'] != $arrStat['B']
&& $arrStat['A'] != $arrStat['C']
&& $arrStat['D'] != $arrStat['D'] ){
$find = true ;
}
}
if (!$find) {
return false ;
}
//7
$opt7 = array(
'A'=>3,'B'=>2,'C'=>1,'D'=>0,
);
if ($opt7[$arr["_7"]] != abs($arr["_7"]-$arr["_8"])) {
return false;
}
//8
$opt8 = array(
'A'=>0,'B'=>1,'C'=>2,'D'=>3,
);
if ($opt8[$arr['_8']] != $arrStat['A']){
return false;
}
//9
$flag = false;
$count = $arrStat['B'] + $arrStat['C'] + $arrStat['D'] ;
switch($arr['_9']) {
case 'A':
if (in_array($count,array(4,6,8,10))){
$flag = true;
}
break;
case 'B':
if (in_array($count,array(2,3,5,7))){
$flag = true;
}
break;
case 'C':
if ($count < 5){
$flag = true;
}
break;
case 'D':
if (in_array($count,array(1,4,9))){
$flag = true;
}
break;
}
if (!$flag){
return false;
}
//10
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment