Last active
December 23, 2015 11:09
-
-
Save robert-nix/6626091 to your computer and use it in GitHub Desktop.
Simple www.gameofbins.com solver
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
scoreBin = (items) -> | |
failure = 0 | |
color = {} | |
type = {} | |
pattern = {} | |
for item in items | |
color[item.color] or= 0 | |
type[item.type] or= 0 | |
pattern[item.pattern] or= 0 | |
color[item.color]++ | |
type[item.type]++ | |
pattern[item.pattern]++ | |
for c of color | |
if color[c] is 2 | |
failure++ | |
for t of type | |
if type[t] is 2 | |
failure++ | |
for p of pattern | |
if pattern[p] is 2 | |
failure++ | |
if items.length is 3 | |
if failure > 0 | |
-1 | |
else | |
5 | |
else | |
# special evaluations for size 2 bins | |
if failure == 1 # easiest to complete (not verified) | |
0.1 | |
else if failure > 1 # harder to complete | |
-0.1 | |
else | |
0 | |
# negative depth because the queue is backwards | |
# depth = 2 is too much for my machine, 4^n and all. | |
# could definitely get rid of some garbage allocs but meh | |
depth = 3 | |
solve = -> | |
index = 9 | |
queue = BinGame.WindowItems.itemQueue.items | |
binsCopy = (_.clone(b.items) for b in BinGame.WindowItems.bins) | |
step = (i, bins, score) -> | |
if i < depth then return [score, -1] | |
item = queue[i] | |
maxScore = -1/0 | |
maxBin = -1 | |
for j in [0..3] | |
newBins = (b for b in bins) | |
# only clone the bin we're modifying | |
newBins[j] = (s for s in newBins[j]) | |
newBins[j].push item | |
val = scoreBin newBins[j] | |
if Math.abs(val) >= 1 | |
newBins[j] = [] | |
[newScore, nextBin] = step(i - 1, newBins, score + val) | |
if newScore > maxScore | |
maxScore = newScore | |
maxBin = j | |
return [maxScore, maxBin] | |
step index, binsCopy, 0 | |
interval = 0 | |
autosolve = -> | |
interval = setInterval (-> | |
[score, bin] = solve() | |
$el = $('#two-' + (bin + 1).toString 10) | |
$el.click() | |
), 500 | |
stopsolve = -> | |
clearInterval interval |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment