Skip to content

Instantly share code, notes, and snippets.

@windyjonas
Created December 8, 2016 20:55
Show Gist options
  • Save windyjonas/b7d4812f27988c904071c715708bb35b to your computer and use it in GitHub Desktop.
Save windyjonas/b7d4812f27988c904071c715708bb35b to your computer and use it in GitHub Desktop.
Generera julklappslista
<?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