3 ms·
Poor man's attempt: var speed = 50 // Prime the result output for (col of [rin, bin, gin]) { rin.valueAsNumber = 0
by tetris11 3y ago
Poor man's attempt:
var speed = 50
// Prime the result output
for (col of [rin, bin, gin]) {
rin.valueAsNumber = 0
bin.valueAsNumber = 0
gin.valueAsNumber = 0
}
rin.dispatchEvent(new Event('change'));
async function tryit(col, incr) {
// Increment a single color
col.valueAsNumber = col.valueAsNumber + incr
col.dispatchEvent(new Event('change'));
submit.click()
await (new Promise(resolve => setTimeout(resolve, speed)));
var res_text = result.innerText.split(/[ ()%]/)[4]
if (res_text === "Splendid!") {
throw new Error("Finished")
}
return (parseInt(res_text))
}
async function trymany() {
// We need to iterate at least twice due to rounding
// in result percentage, sometimes making neighbouring
// colors have the same result.
var last_res = 0, max_tries = 3;
while (--max_tries > 0) {
for (col of [rin, gin, bin]) {
while (true) {
var new_res = await tryit(col, 1)
if (last_res >= new_res) {
// set last value and break
await tryit(col, -1)
break
}
last_res = new_res
}
}
}
}
await trymany()
- js4ever 3y agoNot bad, 18 to 43 attempts on 10 games I tried. Far from optimal but good enough :p
- tetris11 3y agoI had an updated version which implemented a binary search, but I couldn't update my previous comment so I dropped it. That being said, due to the scoring function `score(r,g,b,rr,gg,bb)` giving the exact same score sometimes for neighbouring colors, the search would still have to repeat and wasn't much more optimal than the code you see above.
- sleepingreset 3y agohow do you run this?
- andy_ppp 3y agoI imagine pasting into the JS console after inspecting the page?
- sleepingreset 3y agonvm thought it was bugged. user issue not code
- deleted 3y ago[deleted]
- deleted 3y ago[deleted]
- sunnynagam 3y agoOptimized with binary search (can't figure out why ycomb formats my code all ugly) var speed = 500; [rin, bin, gin].forEach(col => { col.valueAsNumber = 7; col.dispatchEvent(new Event('change')); col.score = 0; }); async function tryit(col, value) { col.valueAsNumber = value; col.dispatchEvent(new Event('change')); submit.click(); await new Promise(resolve => setTimeout(resolve, speed)); var res_text = result.innerText.split(/[ ()%]/)[4]; if (res_text === "Splendid!") { throw new Error("Finished - Found correct combination"); } col.score = parseInt(res_text); return col.score; } async function binarySearch(col) { let start = 0; let end = 15; let mid = 7; let startAccuracy = await tryit(col, start); let endAccuracy = await tryit(col, end); let midAccuracy = 0; while (true) { mid = Math.floor((start + end) / 2); midAccuracy = await tryit(col, mid); if ((end - start) <= 2) { const max = Math.max([startAccuracy, midAccuracy, endAccuracy]); if (startAccuracy == max) await tryit(col, start); else if (midAccuracy == max) await tryit(col, mid); else await tryit(col, end); return; } if (endAccuracy > startAccuracy) { start = mid; startAccuracy = midAccuracy; } else { end = mid; endAccuracy = midAccuracy; } } } async function findOptimalCombination() { for (const col of [rin, gin, bin]) { await binarySearch(col); } /* rounding */ for (const col of [rin, gin, bin]) { const mid = col.valueAsNumber; const score = await tryit(col, mid); const left = await tryit(col, mid - 1); if (score >= left) { const right = await tryit(col, mid + 1); if (score >= right) await tryit(col, mid); } } console.log("Optimization complete"); } await findOptimalCombination();
- tetris11 3y agoThis is great -- it has a very consistent performance of ~20 steps. I notice that my naive attempt seems to be faster in some cases.