Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
* Unruly Game Solver solution in Clear category for Unruly by JimmyCarlos
import copy
def unruly(A):
if type(A[0]) == str: A = [list(row) for row in A]
H,W = len(A),len(A[0])
while any(A[R][C] == "." for R in range(H) for C in range(W)):
# Rule 1a: Cannot create 3 in a row horizontally
for R in range(H):
for C in range(W-2):
a,b,c = A[R][C],A[R][C+1],A[R][C+2]
if a == b == "B": A[R][C+2] = "W"
if a == c == "B": A[R][C+1] = "W"
if b == c == "B": A[R][C] = "W"
if a == b == "W": A[R][C+2] = "B"
if a == c == "W": A[R][C+1] = "B"
if b == c == "W": A[R][C] = "B"
# Rule 1b: Cannot create 3 in a row vertically
for C in range(W):
for R in range(H-2):
a,b,c = A[R][C],A[R+1][C],A[R+2][C]
if a == b == "B": A[R+2][C] = "W"
if a == c == "B": A[R+1][C] = "W"
if b == c == "B": A[R][C] = "W"
if a == b == "W": A[R+2][C] = "B"
if a == c == "W": A[R+1][C] = "B"
if b == c == "W": A[R][C] = "B"
# Rule 2a: Rows must contain equal amounts of black and white.
for R in range(H):
counts = {"B":0, "W":0, ".":0}
for C in range(W): counts[A[R][C]] += 1
if counts["B"] == W//2:
for C in range(W):
if A[R][C] == ".":
A[R][C] = "W"
if counts["W"] == W//2:
for C in range(W):
if A[R][C] == ".":
A[R][C] = "B"
# Rule 2b: Columns must contain equal amounts of black and white.
for C in range(W):
counts = {"B":0, "W":0, ".":0}
for R in range(H): counts[A[R][C]] += 1
if counts["B"] == H//2:
for R in range(H):
if A[R][C] == ".":
A[R][C] = "W"
if counts["W"] == H//2:
for R in range(H):
if A[R][C] == ".":
A[R][C] = "B"
# Rule 3a: Look at rows with just one of a B/W left. Would certain placements cause a 3-in-a-row?
for R in range(H):
B = [A[R][C] for C in range(W)]
if B.count("B") + 1 == W//2: # Only one Black left, all but one uncoloured tile is White.
for C in range(W-2): # If we have a group of 3 non-blacks in a row, one of them must be black...
if all(B[c] != "B" for c in (C,C+1,C+2)): # ...so everything else must be white!
for c in range(W):
if c not in (C,C+1,C+2) and B[c] == ".":
A[R][c] = "W"
if B.count("W") + 1 == W//2:
for C in range(W-2):
if all(B[c] != "W" for c in (C,C+1,C+2)):
for c in range(W):
if c not in (C,C+1,C+2) and B[c] == ".":
A[R][c] = "B"
# Rule 3b: Look at columns with just one of a B/W left. Would certain placements cause a 3-in-a-row?
for C in range(W):
B = [A[R][C] for R in range(H)]
if B.count("B") + 1 == H//2:
for R in range(H-2):
if all(B[r] != "B" for r in (R,R+1,R+2)):
for r in range(H):
if r not in (R,R+1,R+2) and B[r] == ".":
A[r][C] = "W"
if B.count("W") + 1 == H//2:
for R in range(H-2):
if all(B[r] != "W" for r in (R,R+1,R+2)):
for r in range(H):
if r not in (R,R+1,R+2) and B[r] == ".":
A[r][C] = "B"
# Should python print the grid?
print_the_grid = False
if print_the_grid:
for row in A: print("".join(row))
print()
__import__("time").sleep(0.3)
return ("".join(row) for row in A)
July 26, 2020