Created
December 8, 2016 20:55
-
-
Save windyjonas/b7d4812f27988c904071c715708bb35b to your computer and use it in GitHub Desktop.
Generera julklappslista
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$relations = array( | |
'malin' => array( 'jonas', 'jack' ), | |
'jonas' => array( 'malin', 'jack' ), | |
'anna' => array( 'mats', 'filippa' ), | |
'mats' => array( 'anna', 'filippa' ), | |
'filippa' => array( 'anna', 'mats' ), | |
'gussi' => array( 'stefan', 'emma' ), | |
'stefan' => array( 'gussi' ), | |
'emma' => array( 'gussi', 'ted' ), | |
'ted' => array( 'emma' ), | |
); | |
$people = array_keys( $relations ); | |
$continue = true; | |
$count = 0; | |
while ( $continue && $count < 200 ) { | |
shuffle( $people ); | |
$taken = array(); | |
$buf = ''; | |
$stop = false; | |
foreach ( $people as $person ) { | |
$candidates = array(); | |
foreach ( $people as $candidate ) { | |
if ( $person !== $candidate | |
&& ! in_array( $candidate, $taken ) | |
&& !in_array( $candidate, $relations[ $person ] ) ) { | |
$candidates[] = $candidate; | |
} | |
} | |
if ( empty( $candidates ) ) { | |
$stop = true; | |
} else { | |
$receiver = $candidates[ array_rand( $candidates ) ]; | |
$taken[] = $receiver; | |
$buf .= "$person köper till $receiver\n"; | |
} | |
} | |
if ( ! $stop ) { | |
echo $buf; | |
$continue = false; | |
} | |
$count++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment