M3JVMYW23743DF67CMHY5P6YMYFOH7NBP3XSM4CPMOLZN4IOLRHAC
prev.add(JSON.stringify([p1, p2]));
prev.add(s);
}
if (p1.length == 0) {
outcomesCache[json] = 2;
return false;
}
if (p2.length == 0) {
outcomesCache[json] = 1;
return true;
}
let top1 = p1.splice(0, 1)[0];
let top2 = p2.splice(0, 1)[0];
let winner;
if (p1.length >= top1 && p2.length >= top2) {
// recurse
winner = simulateGame(p1.slice(0, top1), p2.slice(0, top2));
} else if (top1 > top2) {
winner = true;
} else {
winner = false;
if (winner) {
p1.push(top1);
p1.push(top2);
} else {
p2.push(top2);
p2.push(top1);
}
}
}
let res = simulateGame(p1, p2);
cards = [];
if (res) {
cards = p1;
} else {
cards = p2;
}
score = 0;
for (let i = cards.length - 1, j = 1; i >= 0; --i, ++j) {
score += cards[i] * j;
}
console.log(score);