Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
26-liner: almost easy solution in Clear category for Unfair Districts by przemyslaw.daniel
def unfair_districts(number, data):
height, width = len(data), len(data[1])
valid = {(x, y) for y in range(width) for x in range(height)}
out = [['']*width for _ in range(height)]
stack = [((0, 0), [((0,0),)])]
while stack:
(x, y), area = stack.pop()
if len(sum(area, ())) == height*width:
wins, loses = 0, 0
for i in area:
a = sum([data[x][y][0] for x, y in i])
b = sum([data[x][y][1] for x, y in i])
wins, loses = wins+(a > b), loses+(a < b)
if wins <= loses: continue
for k, i in enumerate(area):
for a, b in i: out[a][b] = str(k)
return [''.join(j) for j in out]
last_group = area.pop()
group_sum = sum(sum([data[i][j] for i, j in last_group], []))
if group_sum > number: continue
if group_sum == number: area, last_group = area+[last_group], ()
neighbors = {(x+1, y), (x-1, y), (x, y+1), (x, y-1)} & valid
for i in (neighbors - set(sum(area, ())+last_group)):
stack += [(i, area+[last_group+(i,)])]
if len(last_group) in [1, 2]:
stack += [(last_group[-1], area+[last_group+(i,)])]
July 4, 2017
Comments: