Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Borders solution in Clear category for Color Map by DiZ
def color_map(colored_map):
# Construct all regions
regions = {}
for i, line in enumerate(colored_map):
for j, cell in enumerate(line):
regions.setdefault(cell, set()).add(i + 1j*j)
# Get neigbours for all regions
neighbours = {}
for region, cells in regions.items():
border = set.union(*({c + 1j ** k for k in range(4)} for c in cells)) - cells
neighbours[region] = {r for r, c in regions.items() if border & c}
# Color each region with first available color
c, colors = 0, [0] * len(regions)
while c < len(regions):
variants = set(range(colors[c]+1, 5)) - {colors[n] for n in neighbours[c]}
colors[c] = len(variants) and min(variants)
c += 2*bool(variants) - 1
return colors
Feb. 11, 2015
Comments: