Created
May 11, 2011 19:34
-
-
Save okaq/967144 to your computer and use it in GitHub Desktop.
Solution: Magicka (Google Code Jam 2011 Qualification Round Problem B)
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
/* Solution: Magicka (Google Code Jam 2011 Qualification Round) | |
* created by: [email protected] | |
* on: 05/11/2011 | |
* run: node Bs.js infile outfile | |
*/ | |
var fs = require('fs'); | |
/* argv test | |
console.log('ok'); | |
process.argv.forEach(function(val, index, array) { | |
console.log(index + ': ' + val); | |
}); | |
console.log('cwd: ' + process.cwd()); | |
*/ | |
// read infile | |
// var fin = fs.openSync(process.argv[2], 'r'); | |
var fin = fs.readFileSync(process.argv[2], 'utf-8'); | |
// console.log(fin); | |
// split infile | |
var splt = fin.split('\n'); | |
var numTests = splt[0]; | |
var splts = splt.slice(1, numTests+1); // numTests+0 returns 99, numTests+1 returns 101, cant win ;) | |
splts.pop(); | |
var splt2 = []; | |
splts.forEach(function(val, index, array) { | |
splt2.push(val.split(' ')); | |
}); | |
// console.log(numTests); | |
// console.log(splts.length); | |
// console.log(splt2); | |
// splt2 is list of lists ;) | |
// parse infile | |
var invks = []; | |
for (var i = 0; i < splt2.length; i++) { | |
var invk0 = new invk(); | |
// fill combines list | |
invk0.nC = parseInt(splt2[i][0]); | |
if (invk0.nC > 0) { | |
for (var j = 0; j < invk0.nC; j++) { | |
// invk0.C.push(splt2[i][1+j]); | |
var com0 = splt2[i][1+j]; | |
var b0 = com0.substr(0,2); | |
var nb0 = com0[2]; | |
invk0.C[b0] = nb0; | |
} | |
} | |
// fill transforms list | |
var iT = (invk0.nC == 0) ? 1 : invk0.nC + 1; | |
invk0.nT = parseInt(splt2[i][iT]); | |
if (invk0.nT > 0) { | |
for (var j = 0; j < invk0.nT; j++) { | |
invk0.T.push(splt2[i][iT+1+j]); | |
} | |
} | |
// fill elements list | |
var iE = iT + invk0.nT + 1; | |
invk0.nE = parseInt(splt2[i][iE]); | |
invk0.sE = splt2[i][iE+1]; | |
if (invk0.nE > 0) { | |
var ea = []; | |
for (var j = 0; j < invk0.nE; j++) { | |
ea.push(invk0.sE[j]); | |
} | |
invk0.E = ea; | |
} | |
invks.push(invk0); | |
} | |
//console.log(invks); | |
// run sim | |
var sim = []; | |
for (var i = 0; i < invks.length; i++) { | |
// rules | |
// elements combine (ab) | (ba) = c | |
// elements transform (a***b) = [] | |
var sp0 = []; | |
sp0[0] = invks[i].E[0]; | |
// transform needs to be placed in hash for each transform | |
var tC = 0; // matches with transform, when reaches 2, clear list | |
var tH = {}; | |
for (var j = 0; j < invks[i].nT; j++) { | |
tH[invks[i].T[j]] = [0,0]; | |
} | |
// if (invks[i].T.indexOf(sp0[0]) != -1) { | |
//tC += 1; | |
//} | |
for (var j = 0; j < invks[i].nT; j++) { | |
var ti0 = invks[i].T[j].indexOf(sp0[0]); | |
if (ti0 != -1) { | |
tH[invks[i].T[j]][ti0] += 1 | |
} | |
} | |
for (var j = 1; j < invks[i].nE; j++) { | |
sp0[sp0.length] = invks[i].E[j]; // sp0.push(invks[i].E[j]); | |
var ab = sp0[sp0.length-1] + sp0[sp0.length-2]; | |
var ba = sp0[sp0.length-2] + sp0[sp0.length-1]; | |
// for (var k = 0; k < invks[i].nC; k++) { | |
// var c0 = invks[i].C[k]; | |
// c0 = c0.substr(0, 2); | |
// } | |
var c0 = invks[i].C[ab] || invks[i].C[ba]; | |
if (c0 != undefined) { | |
for (var k = 0; k < invks[i].nT; k++) { | |
var ti0 = invks[i].T[k].indexOf(sp0[sp0.length-2]); | |
if (ti0 == 0) { | |
tH[invks[i].T[k]][0] -= 1; // .push(ti0); | |
} | |
if (ti0 == 1) { | |
tH[invks[i].T[k]][1] -= 1; | |
} | |
} | |
sp0[sp0.length-2] = c0; | |
sp0.pop(); | |
/* only bases transform | |
// if (invks[i].T.indexOf(c0) != -1) { | |
// tC += 1; | |
// } | |
for (var k = 0; k < invks[i].nT; k++) { | |
var ti0 = invks[i].T[k].indexOf(c0); | |
if (ti0 != -1) { | |
tH[invks[i].T[j]].push(c0); | |
} | |
} | |
for (var k = 0; k < invks[i].nT; k++) { | |
if (tH[invks[i].T[k]].indexOf(0) != -1 && tH[invks[i].T[k]].indexOf(1) != -1) { | |
sp0 = []; | |
tH = {}; | |
for (var m = 0; m < invks[i].nT; m++) { | |
tH[invks[i].T[m]] = []; | |
} | |
break; | |
} | |
} | |
*/ | |
continue; | |
} | |
// transform test | |
// if (invks[i].T.indexOf(invks[i].E[j]) != -1) { | |
// tC += 1; | |
// } | |
for (var k = 0; k < invks[i].nT; k++) { | |
var ti0 = invks[i].T[k].indexOf(invks[i].E[j]); | |
if (ti0 == 0) { | |
tH[invks[i].T[k]][0] += 1; // .push(ti0); | |
} | |
if (ti0 == 1) { | |
tH[invks[i].T[k]][1] += 1; | |
} | |
} | |
// if (tC >= 2) { | |
// sp0 = []; | |
// } | |
for (var k = 0; k < invks[i].nT; k++) { | |
if (tH[invks[i].T[k]][0] > 0 && tH[invks[i].T[k]][1] > 0) { | |
// console.log(sp0); | |
// console.log(tH); | |
sp0 = []; | |
tH = {}; | |
for (var m = 0; m < invks[i].nT; m++) { | |
tH[invks[i].T[m]] = [0,0]; | |
} | |
break; | |
} | |
} | |
// console.log(tH); | |
} | |
sim.push(sp0); | |
} | |
// console.log(sim); | |
// console.log(Array.prototype.indexOf); | |
// invokation set class | |
function invk() { | |
// combines (represented by an object of base/nonbase pairs) | |
this.nC = 0; | |
this.C = {}; | |
// transforms | |
this.nT = 0; | |
this.T = []; | |
// elements | |
this.nE = 0; | |
this.sE = ''; | |
this.E = []; | |
} | |
// write outfile | |
var data = ''; | |
for (var i = 0; i < sim.length; i++) { | |
// var r = Math.random() * 255 >>> 0; | |
data += 'Case #' + (i+1) + ': ' + '[' + sim[i].join(', ') + ']\n'; | |
} | |
fs.writeFileSync(process.argv[3], data, 'utf-8'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment