Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Eaten Go Stones by kurosawa4434
def go_game(board):
width, height = len(board[0]), len(board)
def check_adjacent(point, stone):
y, x = point
ally = set()
for ty, tx in (y-1, x), (y, x+1), (y+1, x), (y, x-1):
if 0 <= ty < height and 0 <= tx < width:
if board[ty][tx] == '+':
return '+'
if board[ty][tx] == stone:
ally.add((ty, tx))
return ally
def alive(y, x, stone):
done_points = {(y, x)}
next_points = {(y, x)}
while next_points:
search_points = next_points
next_points = set()
for p in search_points:
adj_points = check_adjacent(p, stone)
if adj_points == '+':
return True
next_points |= adj_points
next_points -= done_points
done_points |= next_points
return False
result = {'B': 0, 'W': 0}
for y, row in enumerate(board):
for x, stone in enumerate(row):
if stone != '+' and not alive(y, x, stone):
result[stone] += 1
return result
if __name__ == '__main__':
assert go_game(['++++W++++',
'+++WBW+++',
'++BWBBW++',
'+W++WWB++',
'+W++B+B++',
'+W+BWBWB+',
'++++BWB++',
'+B++BWB++',
'+++++B+++']) == {'B': 3, 'W': 4}
print("Coding complete? Click 'Check' to earn cool rewards!")
May 31, 2018