Skip to content

Instantly share code, notes, and snippets.

@kiy0taka
Created February 21, 2013 16:26
Show Gist options
  • Save kiy0taka/5005926 to your computer and use it in GitHub Desktop.
Save kiy0taka/5005926 to your computer and use it in GitHub Desktop.
enum Direction { L,U,R,D }
def debug = { directions, button = null ->
def result = """|3${directions[3]}0
|${directions[2]} ${directions[0]}
|2${directions[1]}1""".stripMargin()
if (button != null) result = result.replace("$button", '*')
result
}
int combinationCount = 0
8.times { combinationCount += 4.power(it+1) }
def result = (0..<combinationCount).findResult { int n ->
def buttons = [n%4]
while ((n = (int) n / 4)) {
buttons << (--n%4)
}
def directions = args.collect { Direction.valueOf(it) }
def history = [debug(directions)]
buttons.each { btn ->
directions.eachWithIndex { d, i ->
directions[i] = i == btn ? d : ++d
}
history << debug(directions, btn)
}
directions == Direction.values() ? history : null
}
println result?.join('\n----\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment