Hi competitive programmers and especially who writes problem solutions in JavaScript. I'm amoung them, yes I'm a little crazy too;)
I've already participated in two contests. In first for div 2 I've managed to solve 2 problems, but in next for div 3 I've just solved 1 and was a little disappointed about that. I decided to skip next one but curriousity took over me and I've looked to first problem in such an epic contest Educational Codeforces Round 146 (Rated for Div. 2). https://codeforces.net/contest/1814/problem/A after some thinking about and scratching some expressions on papare i saw a math model of solution and it looked like to transfer 2 bits over wire and i jumped in contest room but failed( My assumptions were wrong( But thanks to Codeforces I can see other's solutions I've updated my JS code but keep failing. Thanks to shakhnoov I figure it out and now I present you JavaScript solution runner CodeforcesIO and for testing CodeforcesTester. You if you found it helpful for you than I'm really glade! Thank you and happy coding! _
_
class CodeforcesTester {
_inputGenerator
_outputGenerator
* _generateReadline(testCases) {
const arr = testCases.split('\n').filter(s => s);
const lastIndex = arr.length - 1
for (let i = 0; i < lastIndex; i++) {
yield arr[i]
}
return arr[lastIndex]
}
/**
* @param {string} inputFileContent copy input data from problem
* @return {CodeforcesTester}
*/
inputData(inputFileContent) {
this._inputGenerator = this._generateReadline(inputFileContent);
return this
}
/**
* @param {string} outputFileContent copy output data from problem
* @return {CodeforcesTester}
*/
outputData(outputFileContent) {
this._outputGenerator = this._generateReadline(outputFileContent);
return this
}
readline() {
if (this._inputGenerator) return this._inputGenerator.next().value
throw new Error('Input data was not set')
}
print(str) {
if (this._outputGenerator) {
const nextValue = this._outputGenerator.next().value
if (str != nextValue) throw new Error(str + ' <-- Incorrect output line, should be --> ' + nextValue)
}
console.log(str)
}
}
module.exports = { CodeforcesTester };
_