Created
February 4, 2019 02:15
-
-
Save bcherny/0f778173246356d8467112c0365b10a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let {readFile} = require('fs') | |
let tally = {ab: 0, ba: 0} | |
let globalDone = 0 | |
let COUNT = 1000 | |
for (let x = 0; x < COUNT; x++) { | |
let result = '' | |
let done = 0 | |
let checkDone = () => { | |
done++ | |
if (done === 2) { | |
globalDone++ | |
tally[result]++ | |
} | |
if (globalDone === COUNT) { | |
console.log(JSON.stringify(tally, null, 2)) | |
} | |
} | |
readFile('./a.txt', {encoding: 'utf8'}, (error, data) => { | |
result += data | |
checkDone() | |
}) | |
readFile('./b.txt', {encoding: 'utf8'}, (error, data) => { | |
result += data | |
checkDone() | |
}) | |
} | |
/* | |
Output from a few runs: | |
$ node ./index.js | |
{ | |
"ab": 969, | |
"ba": 31 | |
} | |
{ | |
"ab": 988, | |
"ba": 12 | |
} | |
$ node ./index.js | |
{ | |
"ab": 983, | |
"ba": 17 | |
} | |
$ node ./index.js | |
{ | |
"ab": 970, | |
"ba": 30 | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment