def charDiff(String c1, String c2) {
return (c1 as char) - (c2 as char)
}
def decodeCoords(String s) {
return [charDiff(s[0], 'a'), charDiff(s[1], '1')]
}
def checkCoords(coords) {
return ((coords[0] in (0..7)) && (coords[1] in (0..7)))
}
def knightMove(legitKnightPositions) {
def knightMoves = [[1, 2], [2, 1], [2, -1], [1, -2], [-1, -2], [-2, -1], [-2, 1], [-1, 2]]
def result = ([0]*64).collate(8)
(0..<8).each { x ->
(0..<8).each { y ->
if (legitKnightPositions[x][y]) {
knightMoves.each { move ->
def newCoord = [x + move[0], y + move[1]]
if (checkCoords(newCoord)) {
result[newCoord[0]][newCoord[1]] = 1
}
}
}
}
}
return result
}
def draughtsmanMoves(c) {
return [[-1, -1], [1, -1]].collect { [c[0] + it[0], c[1] + it[1]] }.findAll { checkCoords(it) }
}
boolean solve(k, d, move) {
if (d[1] == 0) return false
if (k.flatten().find() == null) return false
if (move == 'white') {
k = knightMove(k)
if (k[d[0]][d[1]]) return true
return solve(k, d, 'black')
}
else {
def moves = draughtsmanMoves(d)
moves.each {
if (it[0] in [1..6]) {
if (k[it[0]][it[1]]) k[it[0]][it[1]] = 0
}
}
def blackWinStrategy = moves.find { solve(k, it, 'white') == false }
return (blackWinStrategy == null)
}
}
//def s = new Scanner('a1 b6 white')
def s = new Scanner(System.in)
def tokens = s.nextLine().split(/\s/).toList()
def (knight, draughtsman) = tokens[0..<2].collect(this.&decodeCoords)
def firstMove = tokens[2]
def knightPos = ([0]*64).collate(8)
knightPos[knight[0]][knight[1]] = 1
println solve(knightPos, draughtsman, firstMove) ? "white wins" : "black wins"
ZGVmIGNoYXJEaWZmKFN0cmluZyBjMSwgU3RyaW5nIGMyKSB7CiAgICByZXR1cm4gKGMxIGFzIGNoYXIpIC0gKGMyIGFzIGNoYXIpCn0KCmRlZiBkZWNvZGVDb29yZHMoU3RyaW5nIHMpIHsKICAgIHJldHVybiBbY2hhckRpZmYoc1swXSwgJ2EnKSwgY2hhckRpZmYoc1sxXSwgJzEnKV0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