Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Color Map solution in Uncategorized category for Color Map by capback250
# migrated from python 2.7
COORDS = ((-1, 0), (0, -1), (0, 1), (1, 0))
from collections import defaultdict as dic
from functools import reduce
def color_map(mapa):
neibors = {}
colored = dic(list)
totalCountres = set(reduce(list.__add__, [list(x) for x in mapa]))
for country in totalCountres:
neibors[country] = sorted(findNeibors(mapa, country))
for currentCountry in totalCountres:
colors = [1,2,3,4]
for neib in neibors[currentCountry]:
if colored[neib] in colors:
del colors[colors.index(colored[neib])]
if colors:
colored[currentCountry] = colors[0]
return [colored[x] for x in totalCountres]
def findNeibors(mapa, country):
neibors = set()
for i1, k1 in enumerate(mapa):
for i2, k2 in enumerate(k1):
if k2 == country:
for c in COORDS:
x, y = i1 + c[0], i2 + c[1]
if (x >= 0 and y >=0) and (x < len(mapa) and y < len(mapa[0])):
if mapa[x][y] != country:
neibors.add(mapa[x][y])
return neibors
Jan. 27, 2016