Esempio n. 1
0
function permutations(array $elements)
{
    if (count($elements) <= 1) {
        (yield $elements);
    } else {
        foreach (permutations(array_slice($elements, 1)) as $permutation) {
            foreach (range(0, count($elements) - 1) as $i) {
                (yield array_merge(array_slice($permutation, 0, $i), [$elements[0]], array_slice($permutation, $i)));
            }
        }
    }
}
Esempio n. 2
0
function resetAndAddMe()
{
    global $permutations;
    global $happiness;
    global $names;
    $permutations = array();
    foreach ($names as $name) {
        $happiness[$name]['Anton'] = 0;
        $happiness['Anton'][$name] = 0;
    }
    $names[] = 'Anton';
    permutations($names);
}
Esempio n. 3
0
function permutations($num, $max)
{
    if ($num == 1) {
        return [[$max]];
    }
    $r = [];
    for ($i = 0; $i <= $max; $i++) {
        foreach (permutations($num - 1, $max - $i) as $j) {
            $r[] = array_merge($j, [$i]);
        }
    }
    return $r;
}
Esempio n. 4
0
function permutations($items, $perms = array())
{
    global $permutations;
    if (empty($items)) {
        $permutations[] = join(',', $perms);
    } else {
        for ($i = count($items) - 1; $i >= 0; --$i) {
            $newitems = $items;
            $newperms = $perms;
            list($foo) = array_splice($newitems, $i, 1);
            array_unshift($newperms, $foo);
            permutations($newitems, $newperms);
        }
    }
}
Esempio n. 5
0
function permutations($items, $perms = [])
{
    if (empty($items)) {
        $return = array($perms);
    } else {
        $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, permutations($newitems, $newperms));
        }
    }
    return $return;
}
Esempio n. 6
0
/**
 * Calculatie the most happiness
 * 
 * @param array $map
 * @return int
 */
function happiness(array $map)
{
    $people = array_keys($map);
    $currentPerson = $people[0];
    $scores = [];
    // Build a array with all possible options
    foreach (permutations(array_slice($people, 1)) as $set) {
        // Add the current person to the set
        $set[] = $currentPerson;
        $left = $currentPerson;
        $key = implode('.', $set);
        $scores[$key] = 0;
        // Build a scores chart
        foreach ($set as $right) {
            $scores[$key] += $map[$left][$right];
            $scores[$key] += $map[$right][$left];
            $left = $right;
        }
    }
    return max($scores);
}
function permutations(array $array, $key = 0, $limit = 8)
{
    static $result;
    $count = count($array);
    try {
        if ($count > $limit) {
            throw new LengthException(sprintf('Error!: Unable to generate permutations with more than %d values', $limit));
        }
        if ($key == $count) {
            $result[] = $array;
        } else {
            for ($i = $key; $i < $count; $i++) {
                list($array[$key], $array[$i]) = array($array[$i], $array[$key]);
                permutations($array, $key + 1);
            }
        }
        return $result;
    } catch (LengthException $e) {
        echo $e->getMessage();
    }
}
Esempio n. 8
0
            if ($value2[0] == $value && $value2[2] == $sitRight) {
                $totHappiness += $value2[1];
            }
        }
    }
    return $totHappiness;
}
$gainLoseArr = [];
$myfile = fopen("day13_input.txt", "r") or die("Unable to open file!");
while (!feof($myfile)) {
    $line = trim(fgets($myfile));
    $lineArr = explode(" ", $line);
    if ($lineArr[2] == "lose") {
        $gainLoseArr[] = [$lineArr[0], "-" . $lineArr[3], trim($lineArr[10], ".")];
    } else {
        $gainLoseArr[] = [$lineArr[0], $lineArr[3], trim($lineArr[10], ".")];
    }
}
fclose($myfile);
$nameArray = [];
foreach ($gainLoseArr as $value) {
    if (!in_array($value[0], $nameArray)) {
        $nameArray[] = $value[0];
    }
}
$possibleSeatArr = permutations(array_values($nameArray));
$totHappinessArr = [];
foreach ($possibleSeatArr as $key => $value) {
    $totHappinessArr[] = calcTable($gainLoseArr, $value);
}
echo max($totHappinessArr);
Esempio n. 9
0
            foreach ($distances as $key => $value) {
                if (in_array($currCity, $value) && in_array($nextCity, $value)) {
                    $thisDistance += $value[2];
                    break;
                }
            }
        }
        $totDistances[] = $thisDistance;
    }
    return max($totDistances);
}
$cities = [];
$distances = [];
$myfile = fopen("day9_input.txt", "r") or die("Unable to open file!");
while (!feof($myfile)) {
    $line = trim(fgets($myfile));
    $lineArr = explode(" ", $line);
    if (!in_array($lineArr[0], $cities)) {
        $cities[] = $lineArr[0];
    }
    if (!in_array($lineArr[2], $cities)) {
        $cities[] = $lineArr[2];
    }
    $distances[] = [$lineArr[0], $lineArr[2], $lineArr[4]];
}
fclose($myfile);
$permArr = permutations(array_values($cities));
if (!in_array($cities, $permArr)) {
    $permArr[] = $cities;
}
echo calcShortest($permArr, $distances);
Esempio n. 10
0
function permutations($first = '', $arr, &$results = array())
{
    $len = count($arr);
    if ($len == 1) {
        $results[] = $first . "," . $arr[0];
    } else {
        for ($i = 0; $i < $len; $i++) {
            $tmp = $arr[0];
            $arr[0] = $arr[$i];
            $arr[$i] = $tmp;
            permutations($first . "," . $arr[0], array_slice($arr, 1), $results);
        }
    }
}