Skip to content

Instantly share code, notes, and snippets.

@atomer
Created September 12, 2011 13:55
Show Gist options
  • Save atomer/1211308 to your computer and use it in GitHub Desktop.
Save atomer/1211308 to your computer and use it in GitHub Desktop.
GDD2011 DevQuiz 一人ゲーム(×)
var fs = require("fs"), fileName;
// 入力ファイルチェック
fileName = process.argv[2];
if (!fileName) {
console.log("no exist file.");
process.exit(1);
}
// 入力データ読み込み
fs.readFile(fileName, "utf-8", function(error, data) {
var testData, answer;
if (!error) {
testData = parse(data);
answer = check(testData);
write(answer + "\n");
} else {
console.log("no exist file");
process.exit(1);
}
});
/**
* データ出力
*/
function write(data) {
fs.writeFile("./answer.txt", data, "utf-8", function(error) {
if (error) {
errorWrite(data);
} else {
console.log("complete");
}
});
}
function errorWrite(data) {
console.error("write file error.");
console.log("[data]:");
console.log(data);
}
/**
* テストケースのみを抽出
*/
function parse(origin) {
var data, d, testData = [],
i, j, ilen, jlen;
data = origin.split("\n");
data.shift();
for (i = 1, ilen = data.length; i < ilen; i+=2) {
d = data[i].split(" ");
for (j = 0, jlen = d.length; j < jlen; j++) {
d[j] = parseInt(d[j]);
}
testData.push(d);
}
return testData;
}
/**
* テストケースを回して答を取得
*/
function check(data) {
var answer = [];
for (var i = 0, len = data.length; i < len; i++) {
answer.push(new Calc(data[i]).test());
}
return answer.join("\n");
}
/**
* テスト
*/
function Calc(data) {
this._count = 0;
this._data = data;
}
Calc.prototype = {
test: function() {
this._count = 0;
this._test(this._data);
this._remove();
return this._count;
},
_test: function(data) {
var nTmp = [],
yTmp = [];
for (var i = 0, len = data.length; i < len; i++) {
if (data[i] % 5 !== 0) {
nTmp.push(data[i]);
} else {
yTmp.push(data[i]);
}
}
if (nTmp.length) {
nTmp = this._half(nTmp);
yTmp.concat(this._test(nTmp));
}
return yTmp;
},
_half: function(data) {
for (var i = 0, len = data.length; i < len; i++) {
data[i] = Math.floor(data[i] / 2);
}
this._count++;
return data;
},
_remove: function() {
this._count++;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment