Skip to content

Instantly share code, notes, and snippets.

@Leko
Created July 19, 2013 18:17
Show Gist options
  • Save Leko/6041219 to your computer and use it in GitHub Desktop.
Save Leko/6041219 to your computer and use it in GitHub Desktop.
New battle programming template in JavaScript
var puts = console.log,
p = require("util").print,
cin = parseInput();
// ここに処理を記述
function parseInput(useSplitSpace) {
var index = 0,
ret = [],
input = require("fs").readFileSync("/dev/stdin", "utf8"),
inputs = input.replace(/\r/g, '').split("\n");
useSplitSpace = useSplitSpace || false;
// 入力を改行/空白で区切り平坦な配列に変換
inputs.forEach(function(val) {
if ( useSplitSpace && val !== "" ) {
ret.push(val);
} else {
val.split(" ").forEach(function(el) {
if ( el !== "" ) ret.push(el);
});
}
});
return {
hasNext: function(val) {
if ( typeof val === "undefined" ) {
return typeof ret[index] !== "undefined";
} else {
return ret[index] === val;
}
},
next: function() {
if ( typeof ret[index+1] === "undefined" ) throw new RangeError("Index out of bounds at '" + index+1 + "'");
return ret[index++];
},
nextInt: function() {
return parseInt(this.nextNumber());
},
nextNumber: function () {
if ( !/^\d$/.test(this.top()) ) throw new TypeError("'" + this.top() + "' cannot convet to Number");
return +this.next();
},
top: function() {
return ret[index];
},
rewind: function() {
index = 0;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment