/**
 * PHP Multi Dimensional Array Combinations.
 *
 * 
 */
function combos($data, &$all = array(), $group = array(), $val = null, $i = 0)
{
    if (isset($val)) {
        array_push($group, $val);
    }
    if ($i >= count($data)) {
        array_push($all, $group);
    } else {
        foreach ($data[$i] as $v) {
            combos($data, $all, $group, $v, $i + 1);
        }
    }
    return $all;
}
function combos($arr, $k)
{
    if ($k == 0) {
        return array(array());
    }
    if (count($arr) == 0) {
        return array();
    }
    $head = $arr[0];
    $combos = array();
    $subcombos = combos($arr, $k - 1);
    foreach ($subcombos as $subcombo) {
        array_unshift($subcombo, $head);
        $combos[] = $subcombo;
    }
    array_shift($arr);
    $combos = array_merge($combos, combos($arr, $k));
    return $combos;
}
function combos($data, $colRules, $noOfRows, $noOfCols, $startTime, $gridName, &$counter = 0, $group = array(), $val = null, $i = 0)
{
    if (isset($val)) {
        array_push($group, $val);
    }
    if ($i >= count($data)) {
        // $group = Indexes for all rows
        $row = array();
        for ($j = 0; $j < $noOfRows; $j++) {
            array_push($row, readFromFile($gridName, $j, $group[$j]));
        }
        $counter++;
        if ($counter >= 1000000) {
            if ($counter % 1000000 == 0) {
                echo $counter . " (" . (microtime_float() - $startTime) . ")" . PHP_EOL;
            }
        }
        //
        // if($counter > 5000) {
        // 	if(($counter % 5000) == 0) {
        // 		sleep(2);
        // 		echo "Sleeping for 2 seconds ... snore".PHP_EOL;
        // 	}
        // }
        //var_dump($row);
        //var_dump($group);
        if (checkSuccess($row, $colRules, $noOfRows, $noOfCols)) {
            echo "<div style=\"float:right\">Solution Found<br />";
            $endTime = microtime_float();
            echo "END:" . $endTime . "<br />";
            echo "START:" . $startTime . "<br />";
            echo " - Time taked " . ($endTime - $startTime) . "</div>";
            exit;
        }
    } else {
        foreach ($data[$i] as $v) {
            combos($data, $colRules, $noOfRows, $noOfCols, $startTime, $gridName, $counter, $group, $v, $i + 1);
        }
    }
}
/**
 * Generate solution for race exercise
 *
 * Number of people (n) is having a race. After the race n statements are given 
 * regarding the winner, among which 1 is true and the rest is false. Generate a
 * possible solution which is possible to solve.
 *
 * @param int $n Number of people.
 *
 * @return array $solution Solution for race (statements of participants).
 */
function RaceSolution($n)
{
    $people = range(1, $n);
    $options = array(TRUE, FALSE);
    $names_all = combos($people, $n);
    // List of people appearing in statements.
    $statements_all = combos($options, $n);
    // Whether people won or not.
    shuffle($names_all);
    shuffle($statements_all);
    foreach ($names_all as $names) {
        foreach ($statements_all as $statements) {
            if (RaceCheckStatements($names, $statements)) {
                $hasSolution = FALSE;
                foreach ($people as $true) {
                    // Who was right?
                    $winner = RaceCheckSolution($names, $statements, $true);
                    if ($winner) {
                        if ($hasSolution) {
                            // multiple solutions!
                            break;
                        } else {
                            $solution['names'] = $names;
                            $solution['statements'] = $statements;
                            $solution['true'] = $true;
                            $solution['winner'] = $winner;
                            $hasSolution = TRUE;
                        }
                    }
                }
                if ($hasSolution) {
                    return $solution;
                }
            }
        }
    }
    return NULL;
}
 /**
  * Make Combinations for specified resource.
  *
  * @param  int  $id
  * @return Response
  */
 public function combine($id, Request $request)
 {
     $groups = $request->input('groups');
     // Validate: $groups ahold not be empty, and values should match options table
     // http://laravel.io/forum/11-12-2014-how-to-validate-array-input
     // https://www.google.es/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8&client=ubuntu#q=laravel%20validate%20array%20input
     $product = $this->product->findOrFail($id);
     // Start Combinator machime!
     $data = array();
     foreach ($groups as $group) {
         $data[] = \App\Option::where('option_group_id', '=', $group)->orderby('position', 'asc')->lists('id');
     }
     $combos = combos($data);
     foreach ($combos as $combo) {
         $combination = \App\Combination::create(array());
         $product->combinations()->save($combination);
         $combination->options()->attach($combo);
     }
     // ToDo: Combinations are created with stock = 0. So, set main product stock = 0 too!
     // El stock del producto principal es la suma de los stocks de las combinaciones!
     return redirect('products')->with('success', l('This record has been successfully combined &#58&#58 (:id) ', ['id' => $id], 'layouts'));
 }
Example #6
0
        $return = array();
        for ($i = count($items) - 1; $i >= 0; --$i) {
            $newitems = $items;
            $newperms = $perms;
            list($foo) = array_splice($newitems, $i, 1);
            array_unshift($newperms, $foo);
            $return = array_merge($return, pc_permute($newitems, $newperms));
        }
    }
    return $return;
}
$keys = array();
for ($i = 0; $i < count($possibleRows); $i++) {
    array_push($keys, array_keys($possibleRows[$i]));
}
$keySet = combos($keys);
for ($i = 0; $i < count($keySet); $i++) {
    $row = array();
    for ($j = 0; $j < $NO_OF_ROWS; $j++) {
        array_push($row, $possibleRows[$j][$keySet[$i][$j]]);
    }
    if (checkSuccess($row, $colRules, $NO_OF_ROWS, $NO_OF_COLS)) {
        echo "<div style=\"float:right\">Solution Found<br />";
        $endTime = microtime_float();
        echo "END:" . $endTime . "<br />";
        echo "START:" . $startTime . "<br />";
        echo " - Time taked " . ($endTime - $startTime) . "</div>";
        exit;
    }
}
echo "<div style=\"float:right\">No Solution Found</div>";
            array_unshift($newperms, $foo);
            $return = array_merge($return, pc_permute($newitems, $newperms));
        }
    }
    return $return;
}
$keys = array();
for ($i = 0; $i < $NO_OF_ROWS; $i++) {
    $indexArray = array();
    $rowCount = getRowCountFromFile($gridName, $i);
    for ($j = 0; $j < $rowCount; $j++) {
        $indexArray[$j] = $j;
    }
    array_push($keys, $indexArray);
}
combos($keys, $colRules, $NO_OF_ROWS, $NO_OF_COLS, $startTime, $gridName);
echo "<div style=\"float:right\">No Solution Found</div>";
exit;
function checkSuccess($set, $colRules, $rowCount, $columnCount)
{
    $output = array();
    $match = 0;
    for ($i = 0, $index = 0; $i < $columnCount; $i++) {
        $output[$index] = 0;
        for ($j = 0; $j < $rowCount; $j++) {
            if ($set[$j][$i] == "1") {
                $output[$index]++;
            } else {
                if ($output[$index] != 0) {
                    $index++;
                    $output[$index] = 0;