Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Unruly by kazuki.h
import re
another_color = {"B": "W", "W": "B"}
def three_change(line, color, num):
line = re.sub("\."+color+color, another_color[color]+color+color, line)
line = re.sub(color+"\."+color, color+another_color[color]+color, line)
line = re.sub(color+color+"\.", color+color+another_color[color], line)
return line
def count_change(line, color, half_num):
return line.replace(".", another_color[color]) if line.count(color) == half_num else line
def missing_one_change(line, color, half_num):
if line.count(color) == half_num - 1 and line.count(".") >= 3:
another_c = another_color[color]
____ = re.search("\.\.\.\.", line)
if ____:
start = ____.start()
end = ____.end()
return line[:start].replace(".", another_c)+another_c+".."+another_c+line[end:].replace(".", another_c)
c__ = re.search(another_c+"\.\.", line)
if c__:
start = c__.start()
end = c__.end()
return line[:start].replace(".", another_c)+line[start:end]+line[end:].replace(".", another_c)
_c_ = re.search("\."+another_c+"\.", line)
if _c_:
start = _c_.start()
end = _c_.end()
return line[:start].replace(".", another_c)+line[start:end]+line[end:].replace(".", another_c)
__c = re.search("\.\."+another_c, line)
if __c:
start = __c.start()
end = __c.end()
return line[:start].replace(".", another_c)+line[start:end]+line[end:].replace(".", another_c)
___ = re.search("\.\.\.", line)
if ___:
start = ___.start()
end = ___.end()
return line[:start].replace(".", another_c)+line[start:end]+line[end:].replace(".", another_c)
return line
def line_change(line, num , half_num):
for color in ["B", "W"]:
if "." in line: line = count_change(line, color, half_num)
if "." in line: line = missing_one_change(line, color, half_num)
if "." in line: line = three_change(line, color, num)
return line
from itertools import product
def unruly(grid):
n = len(grid)
half_n = n//2
m = len(grid[0])
half_m = m//2
grid = list(grid)
while any(line.count(".")!=0 for line in grid):
for i in range(n):
line = grid[i]
line = line_change(line, m, half_m)
grid[i] = line
for j in range(m):
line = "".join(grid[r][j] for r in range(n))
line = line_change(line, n, half_n)
grid = [grid[k][:j]+line[k]+grid[k][j+1:] for k in range(n)]
return grid
Dec. 13, 2021