Skip to content

Instantly share code, notes, and snippets.

@renskiy
Last active September 11, 2016 15:54
Show Gist options
  • Select an option

  • Save renskiy/ef608282fefa57f8e528c5cb6970b292 to your computer and use it in GitHub Desktop.

Select an option

Save renskiy/ef608282fefa57f8e528c5cb6970b292 to your computer and use it in GitHub Desktop.
diabolocom
(() => {
let data = [];
process.stdin.setEncoding('utf8');
process.stdin.on('data', data.push.bind(data));
process.stdin.on('end', () => {
let words = {};
data.join().split(/\W+/).filter((word) => {
if (! word) return false;
word = word.toLowerCase();
words[word] = (words[word] || 0) + 1;
return words[word] == 1;
}).sort((word1, word2) => {
return word1.localeCompare(word2);
}).forEach((word) => {
word = word.toLowerCase();
console.log(`${word}: ${words[word]}`);
});
});
})();
(() => {
let analyzer = (function *() {
let logs,
max_calls_start,
max_calls_end,
max_calls = 0,
end_stack = []; // it may be more useful if used something like this: https://github.com/shinout/SortedList
while (logs = yield) {
for (let log of logs) {
if (! log) continue;
let parsed_log = log.match(/(\d+):(\d+)/);
if (parsed_log === null) throw `Unknown log format: ${log}`;
let [start, end] = parsed_log.slice(1).map(Number);
end_stack = end_stack.filter((end_call) => {
return end_call > start;
});
end_stack.push(end);
if (end_stack.length > max_calls) {
max_calls_start = start;
max_calls_end = Math.min(...end_stack);
max_calls = end_stack.length;
}
}
}
console.log(
`The peak for this call log is ${max_calls} simultaneous calls, `
+ `that occurred between ${max_calls_start} and ${max_calls_end}`
);
})();
analyzer.next(); // "start" analyzer
let last_log_chunk = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', (chunk) => {
let logs = chunk.split(/\s+/);
logs[0] = last_log_chunk + logs[0];
last_log_chunk = logs.pop();
analyzer.next(logs);
});
process.stdin.on('end', () => {
analyzer.next([last_log_chunk]);
analyzer.next(); // close analyzer
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment