Skip to content

Instantly share code, notes, and snippets.

@donjajo
Last active September 6, 2017 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donjajo/10fe13ec2067bb22136f183f1bd5177d to your computer and use it in GitHub Desktop.
Save donjajo/10fe13ec2067bb22136f183f1bd5177d to your computer and use it in GitHub Desktop.
Wilfred's Algorithm in PHP
<?php
$you = 1;
$p = 2;
$f = [ $you, $p ];
// Random conversation
$conv = mt_rand( 0, 3 );
// At the beginning set the last date of conversation
$cur_date = '2017-02-06';
$x[ $p ][ $you ] = [ $cur_date => $conv ];
$x[ $you ][ $p ] = [ $cur_date => $conv ];
if( !empty( $x[ $you ][ $p ][ $cur_date ] ) ) {
$interaction_dates = array_keys( $x[ $you ][ $p ] );
// Get last date of conversation
$last_date = $interaction_dates[ count( $interaction_dates ) - 1 ];
// Get month interval
$year_interval = date_diff( date_create( $last_date ), date_create( date( 'Y-m-d' ) ) );
$year_interval = $year_interval->format( '%y' );
if( $year_interval >= 1 ) {
// Can't check usefulness in codes, so I will go ahead and delete P :D
unset( $f[ $p ] );
echo 'Its less than 12months of chats. P is unfriended';
}
else {
// You and P keep conversing and once P says 0 in a row, it means he is silent, probably dead.
$silence = 0;
while( $silence < 3 ) {
$conv = mt_rand( 0, 3 );
if( $conv == 0 )
$silence++;
else
$silence = 0;
echo 'P responded with ' . $conv . '<br />';
$x[ $you ][] = [ date( 'Y-m-d' ) => $conv ];
}
echo 'P is dead, has not responded thrice';
}
}
else {
echo 'P never responded first message. He is not interested, P is unfriended!';
unset( $f[ $p ] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment