-
Notifications
You must be signed in to change notification settings - Fork 0
/
givesto.php
56 lines (43 loc) · 1.19 KB
/
givesto.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
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;
}
function fancy_print($givesto){
echo "Gift Giving List ".@date("Y")."\n";
echo "------------------------\n";
foreach($givesto as $giver => $recipient){
echo strtoupper($giver)." gives to ".strtoupper($recipient)."\n";
}
echo "\n\n";
}
function flatten(array $array) {
$return = array();
array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
return $return;
}