Ejemplo n.º 1
0
function generate_gives_to($group_list, $attempt = 0)
{
    $recipients = [];
    $givesto = [];
    $attempt += 1;
    foreach ($group_list as $k1 => $group) {
        $possible_recipients = array_merge(array_slice($group_list, 0, $k1), array_slice($group_list, $k1 + 1));
        $possible_recipients = array_diff(flatten($possible_recipients), $recipients);
        foreach ($group as $k2 => $person) {
            if (empty($possible_recipients)) {
                if ($attempt < 10) {
                    return generate_gives_to($group_list, $attempt);
                } else {
                    echo " - INCOMPLETE SOLUTION - ";
                }
            }
            $k = array_rand($possible_recipients);
            $givesto[$person] = $possible_recipients[$k];
            $recipients[] = $possible_recipients[$k];
            unset($possible_recipients[$k]);
        }
    }
    fancy_print($givesto);
    return $givesto;
}
Ejemplo n.º 2
0
function generate_gives_to($group_list, $fancy_print = true, $attempt = 0)
{
    $recipients = [];
    $givesto = [];
    $attempt += 1;
    foreach ($group_list as $k1 => $group) {
        $possible_recipients = array_merge(array_slice($group_list, 0, $k1), array_slice($group_list, $k1 + 1));
        $possible_recipients = array_diff(flatten($possible_recipients), $recipients);
        foreach ($group as $k2 => $person) {
            if (empty($possible_recipients)) {
                if ($attempt < 10) {
                    return generate_gives_to($group_list, $fancy_print, $attempt);
                } else {
                    return false;
                }
            }
            $k = array_rand($possible_recipients);
            $givesto[trim($person)] = trim($possible_recipients[$k]);
            $recipients[] = $possible_recipients[$k];
            unset($possible_recipients[$k]);
        }
    }
    //echo fancy_print($givesto);
    //return $givesto;
    return $fancy_print ? fancy_print($givesto) : json_encode($givesto);
}
Ejemplo n.º 3
0
<?php

include 'givesto.php';
$group_list = [['John', 'Paul', 'George', 'Ringo'], ['Elmo', 'Oscar', 'Big Bird', 'Bert'], ['Larry', 'Curly', 'Moe']];
generate_gives_to($group_list);
Ejemplo n.º 4
0
<?php

include 'givesto.php';
$groups = explode("\n", $_GET['input']);
$group_list = [];
foreach ($groups as $k => $group_string) {
    $group_list[$k] = explode(",", $group_string);
}
if (is_null($group_list)) {
    // bad input
    http_response_code(400);
    exit;
}
$result = generate_gives_to($group_list, false);
if ($result === false) {
    http_response_code(400);
    echo "Incomplete Solution - Try adding another group";
    exit;
}
http_response_code(200);
print_r($result);