Skip to content

Instantly share code, notes, and snippets.

@lennym
Created November 26, 2019 23:51
Show Gist options
  • Save lennym/2f944393801e30851aa612ac037631e4 to your computer and use it in GitHub Desktop.
Save lennym/2f944393801e30851aa612ac037631e4 to your computer and use it in GitHub Desktop.
Parkrun Finish Modelling
const log = require('terminal-overwrite');
const toSeconds = t => {
const [minutes, seconds] = t.split(':');
return (minutes * 60) + (+seconds);
};
const TOKEN_RATE = 1.5;
const TOKEN_SWAP = 3;
const FINISHERS = 800;
let times = require('./times.json').map(toSeconds);
while (times.length < FINISHERS) {
const rando = Math.round(Math.random() * times.length);
times.splice(rando, 0, times[rando]);
}
while (times.length > FINISHERS) {
const rando = Math.round(Math.random() * times.length);
times.splice(rando, 1);
}
console.log(`Modelling with ${times.length} participants`);
let queue = [];
let time = 17 * 60; // no finishers before 17 mins
let maxQueue = 0;
let maxWait = 0;
let tokens = 0;
const interval = setInterval(() => {
if (times.length || queue.length) {
time += TOKEN_RATE;
tokens++;
if (tokens % 20 === 0) {
time += TOKEN_SWAP;
}
const incoming = times.filter(t => t < time);
queue = queue.concat(incoming);
times = times.filter(t => t >= time);
maxWait = Math.max(maxWait, queue.length ? (time - queue[0]) : 0);
maxQueue = Math.max(maxQueue, queue.length || 0);
const mins = Math.floor(time / 60);
const secs = Math.round(time % 60);
log(`${mins}:${secs < 10 ? '0' + secs : secs}: ${Array(queue.length).fill('x').join('')}`);
queue.shift();
} else {
clearInterval(interval);
console.log(`Longest queue: ${maxQueue}`);
console.log(`Longest wait: ${Math.round(maxWait)}s`);
}
}, 10);
["17:22","17:24","17:32","17:56","18:13","18:14","18:16","18:24","18:41","18:49","18:50","18:54","18:59","18:59","19:05","19:06","19:15","19:17","19:36","19:39","19:40","19:55","19:58","19:59","20:00","20:03","20:09","20:23","20:24","20:25","20:27","20:28","20:39","20:41","20:43","20:49","20:49","20:50","20:51","20:54","21:00","21:02","21:03","21:04","21:07","21:08","21:09","21:12","21:12","21:17","21:19","21:24","21:25","21:27","21:28","21:29","21:31","21:35","21:38","21:39","21:40","21:40","21:42","21:42","21:43","21:47","21:49","21:50","21:52","21:53","21:56","21:56","21:58","22:00","22:02","22:03","22:04","22:05","22:06","22:08","22:09","22:11","22:14","22:16","22:18","22:18","22:20","22:20","22:22","22:23","22:25","22:26","22:27","22:29","22:30","22:31","22:32","22:33","22:37","22:38","22:40","22:40","22:41","22:43","22:44","22:45","22:47","22:48","22:50","22:52","22:52","22:53","22:56","22:57","23:02","23:03","23:05","23:05","23:06","23:07","23:08","23:15","23:17","23:19","23:19","23:21","23:22","23:23","23:23","23:24","23:29","23:32","23:34","23:39","23:40","23:41","23:41","23:42","23:43","23:45","23:47","23:49","23:49","23:51","23:52","23:53","23:54","23:55","23:56","23:57","24:01","24:02","24:05","24:06","24:08","24:09","24:10","24:11","24:11","24:11","24:12","24:13","24:14","24:14","24:15","24:15","24:16","24:18","24:19","24:21","24:22","24:22","24:23","24:24","24:25","24:26","24:27","24:28","24:28","24:29","24:30","24:33","24:35","24:36","24:36","24:37","24:37","24:38","24:39","24:41","24:42","24:44","24:45","24:46","24:47","24:49","24:50","24:50","24:51","24:54","24:55","24:56","24:57","24:59","25:06","25:07","25:08","25:09","25:09","25:10","25:11","25:14","25:16","25:17","25:22","25:24","25:25","25:26","25:28","25:29","25:30","25:32","25:34","25:34","25:35","25:36","25:37","25:39","25:39","25:40","25:41","25:42","25:44","25:45","25:46","25:47","25:48","25:49","25:49","25:50","25:50","25:51","25:52","25:53","25:54","25:56","25:57","25:58","26:00","26:01","26:04","26:05","26:06","26:07","26:08","26:10","26:11","26:12","26:13","26:14","26:16","26:16","26:18","26:18","26:21","26:23","26:26","26:27","26:27","26:28","26:29","26:29","26:30","26:30","26:31","26:31","26:32","26:35","26:36","26:36","26:37","26:39","26:39","26:41","26:41","26:41","26:43","26:45","26:45","26:46","26:49","26:50","26:50","26:50","26:51","26:51","26:52","26:53","26:53","26:57","26:57","26:58","26:58","26:59","26:59","27:03","27:04","27:05","27:06","27:07","27:07","27:09","27:09","27:12","27:12","27:13","27:13","27:14","27:14","27:15","27:15","27:20","27:22","27:23","27:23","27:24","27:25","27:26","27:28","27:29","27:31","27:32","27:33","27:36","27:39","27:41","27:42","27:44","27:44","27:45","27:47","27:48","27:49","27:50","27:51","27:52","27:52","27:54","27:55","27:56","27:57","27:59","28:00","28:02","28:03","28:05","28:06","28:07","28:08","28:09","28:11","28:12","28:13","28:14","28:14","28:16","28:17","28:18","28:22","28:23","28:24","28:25","28:26","28:26","28:27","28:28","28:30","28:30","28:31","28:32","28:34","28:36","28:39","28:41","28:42","28:43","28:46","28:48","28:49","28:52","28:53","28:54","28:56","28:57","28:58","28:59","28:59","29:00","29:05","29:07","29:08","29:09","29:13","29:14","29:16","29:19","29:20","29:21","29:22","29:23","29:23","29:23","29:24","29:25","29:26","29:27","29:28","29:29","29:33","29:34","29:40","29:42","29:43","29:45","29:48","29:49","29:50","29:51","29:55","29:56","29:57","29:58","29:59","29:59","30:00","30:00","30:02","30:03","30:03","30:05","30:06","30:07","30:11","30:17","30:18","30:18","30:21","30:24","30:25","30:26","30:26","30:28","30:30","30:31","30:34","30:38","30:38","30:39","30:40","30:40","30:41","30:44","30:51","30:57","31:02","31:03","31:04","31:06","31:07","31:07","31:08","31:08","31:09","31:10","31:13","31:14","31:15","31:16","31:16","31:17","31:18","31:23","31:24","31:25","31:26","31:26","31:29","31:30","31:31","31:32","31:38","31:40","31:44","31:45","31:46","31:46","31:49","31:50","31:51","31:52","31:53","31:54","31:57","31:58","32:01","32:04","32:16","32:17","32:18","32:21","32:26","32:31","32:32","32:35","32:35","32:37","32:37","32:45","32:57","33:00","33:01","33:02","33:03","33:33","33:36","33:44","33:47","33:50","33:51","33:55","33:57","34:01","34:03","34:07","34:07","34:08","34:09","34:10","34:11","34:12","34:13","34:13","34:15","34:17","34:23","34:25","34:29","34:33","34:39","34:40","34:43","34:44","34:55","34:58","35:03","35:07","35:13","35:24","35:28","35:29","35:35","35:41","35:50","35:57","36:11","36:12","36:19","36:20","36:30","36:32","36:40","36:44","36:44","36:45","36:49","36:51","36:52","36:53","36:55","36:56","37:00","37:03","37:30","37:34","37:36","38:02","38:05","38:28","39:06","39:08","39:20","39:55","39:59","40:16","41:20","41:43","42:33","44:33","45:33","47:01","47:57","48:28"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment