-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimj_get_best_track.php
71 lines (59 loc) · 1.7 KB
/
timj_get_best_track.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/**
* The "get the best weighted track" part of the This is My Jelly hack.
*
* N.B. tracks are always in the form as [<artist>, <track>].
*
* @author Sam Smith <samuel.david.smith@gmail.com>
*/
/**
* Weights the tracks according to the number of occurances of the track in the
* set.
*
* @param array[array[]]
* @return array[array[]]
*/
function timj_weight_tracks($tracks) {
$groups = array();
$maxSize = 0;
foreach ($tracks as $track) {
$hash = md5(strtolower("{$track[1]} by {$track[0]}"));
$groups[$hash][] = $track;
if (count($groups[$hash]) > $maxSize) {
$maxSize = count($groups[$hash]);
}
}
$result = array();
foreach ($groups as $group) {
$track = $group[0];
$count = count($group);
$track['usernames'] = array();
foreach ($group as $groupMember) {
if (in_array($groupMember['username'], $track['usernames'])) {
--$count;
} else {
$track['usernames'][] = $groupMember['username'];
}
}
$track['weight'] = $count / $maxSize;
$result[] = $track;
}
return $result;
}
/**
* Gets the "best" track from the set of tracks.
*
* @param array[array[]]
* @return array[] A single, solitary, track... ALONE!!!
*/
function timj_get_best_track($tracks) {
$weightedTracks = timj_weight_tracks($tracks);
usort($weightedTracks, function($left, $right) {
if ($left['weight'] == $right['weight']) {
return 0;
}
return $left['weight'] < $right['weight'] ? -1 : 1;
});
$bestTrack = end($weightedTracks);
return $bestTrack;
}