Created
August 16, 2018 15:23
-
-
Save lesiki/ccd700d931c15048eb8725b75629a213 to your computer and use it in GitHub Desktop.
Sokoban Solver
This file contains hidden or 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
def originalNumbers = [6,3,5,4,2] | |
def ops = ['x', '-', '-', '+'] | |
def target = 21 | |
def combine(a, b, op) { | |
if(op == '+') { | |
return a + b | |
} | |
else if (op == '-') { | |
return a - b | |
} | |
else { | |
return a * b | |
} | |
} | |
def currentRouteResult = -1 | |
def route | |
while(currentRouteResult != target) { | |
route = [] | |
def numbersOnThisRoute = originalNumbers.clone() | |
def opsForThisRoute = ops.clone() | |
while(opsForThisRoute.size() && numbersOnThisRoute.size() > 1) { | |
//println "State ::: ops are ${opsForThisRoute}, numbers are ${numbersOnThisRoute}" | |
Collections.shuffle(opsForThisRoute) | |
Collections.shuffle(numbersOnThisRoute) | |
def op = opsForThisRoute.pop() | |
def a = numbersOnThisRoute.pop() | |
def b = numbersOnThisRoute.pop() | |
def newNumber = combine(a, b, op) | |
numbersOnThisRoute.push(newNumber) | |
//println "Applying op ${a} ${op} ${b}" | |
route << "${a}${op}${b} (${newNumber})".toString() | |
} | |
//println "State ::: ops are ${opsForThisRoute}, numbers are ${numbersOnThisRoute}" | |
if(numbersOnThisRoute.size() == 1) { | |
currentRouteResult = numbersOnThisRoute[0] | |
} | |
} | |
println "We got to ${target} by doing ${route.join(', ')}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment