Skip to content

Instantly share code, notes, and snippets.

@lesiki
Created August 16, 2018 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lesiki/ccd700d931c15048eb8725b75629a213 to your computer and use it in GitHub Desktop.
Save lesiki/ccd700d931c15048eb8725b75629a213 to your computer and use it in GitHub Desktop.
Sokoban Solver
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