Our AAA company is in need of some software to help with logistics: you will be given the width and height of a map, a list of x coordinates and a list of y coordinates of the supply points, starting to count from the top left corner of the map as 0.

Your goal is to return a two dimensional array/list with every item having the value of the distance of the square itself from the closest supply point expressed as a simple integer.

Quick examples:

```python
logistic_map(3,3,[0],[0])
#returns
#[[0,1,2],
# [1,2,3],
# [2,3,4]]
logistic_map(5,2,[0,4],[0,0])
#returns
#[[0,1,2,1,0],
# [1,2,3,2,1]]
```

Remember that our company is operating with trucks, not drones, so you can simply use Manhattan distance. If supply points are present, they are going to be within the boundaries of the map; if no supply point is present on the map, just return `None`/`nil`/`null` in every cell.

```python
logistic_map(2,2,[],[])
#returns
#[[None,None],
# [None,None]]
```

**Note:** this one is taken (and a bit complicated) from a problem a real world AAA company [whose name I won't tell here] used in their interview. It was done by a friend of mine. It is nothing that difficult and I assume it is their own version of the FizzBuzz problem, but consider candidates were given about 30 mins to solve it.

Source: codewars | Difficulty: EASY
Tags: Matrices

Graph algorithms

Data structures

Starter code: def logistic_map(width,height,xs,ys):
	

Solutions:
def logistic_map(width, height, xs, ys):
	return [[min([abs(x - x2) + abs(y - ys[i]) for (i, x2) in enumerate(xs)]) if len(xs) else None for x in range(width)] for y in range(height)]


def logistic_map(w, h, xs, ys):
	md = lambda i, j: min((abs(x - i) + abs(y - j) for (x, y) in zip(xs, ys)), default=None)
	return [[md(i, j) for i in range(w)] for j in range(h)]


from sklearn.neighbors import NearestNeighbors
from itertools import product

def logistic_map(width, height, xs, ys):
	if not xs:
		return [[None] * width for _ in range(height)]
	neigh = NearestNeighbors(1, metric='manhattan')
	neigh.fit(list(zip(ys, xs)))
	it = iter(neigh.kneighbors(list(product(range(height), range(width))))[0])
	return [[next(it)[0] for _ in range(width)] for _ in range(height)]


def logistic_map(n, m, x, y):
	co = [[i, j] for (i, j) in zip(y, x)]
	return [[min([abs(k - i) + abs(l - j) for (k, l) in co]) if co else None for j in range(n)] for i in range(m)]


def smallest_dist(x, y, xs, ys):
	min = None
	for i in range(len(xs)):
		v = abs(xs[i] - x) + abs(ys[i] - y)
		min = (min, v)[min == None or min > v]
	return min

def logistic_map(w, h, xs, ys):
	return [[smallest_dist(x, y, xs, ys) for x in range(w)] for y in range(h)]


def logistic_map(width, height, xs, ys):
	neighbours = lambda c: {(c[0] + i, c[1] + j) for (i, j) in [(0, 1), (1, 0), (0, -1), (-1, 0)] if 0 <= c[0] + i < width and 0 <= c[1] + j < height}
	grid = [[None] * width for _ in range(height)]
	(distance, seen, bag) = (0, set(), set(zip(xs, ys)))
	while bag:
		for (x, y) in bag:
			grid[y][x] = distance
		seen |= bag
		bag = set.union(*map(neighbours, bag)) - seen
		distance += 1
	return grid


def logistic_map(width, height, xs, ys):
	grid = []
	supply = [(xs[i], ys[i]) for i in range(len(xs))]
	for y in range(height):
		grid.append([])
		for x in range(width):
			grid[y].append(min([abs(x - sx) + abs(y - sy) for (sx, sy) in supply]) if supply else None)
	return grid


def logistic_map(width, height, xs, ys):
	supply_stops = zip(*[xs, ys])
	points = [[None for x in range(width)] for x in range(height)]
	for s in supply_stops:
		for i in range(width):
			for j in range(height):
				d_cur = abs(s[0] - i) + abs(s[1] - j)
				d_pnt = points[j][i]
				if d_pnt == None or d_cur < d_pnt:
					points[j][i] = d_cur
	return points


def manhattan_distance(x, y, x2, y2):
	return abs(x - x2) + abs(y - y2)

def logistic_map(width, height, x_list, y_list):
	matrix = [[None for __ in range(width)] for _ in range(height)]
	if not x_list or not y_list:
		return matrix
	supply_points = set(zip(y_list, x_list))
	for (r, row) in enumerate(matrix):
		for (c, col_item) in enumerate(row):
			if col_item != 0:
				matrix[r][c] = min((manhattan_distance(r, c, x2, y2) for (x2, y2) in supply_points))
	return matrix