/ Python / 2018 / 06.py
06.py
 1  from lib import *
 2  
 3  input = read_input(2018, 6)
 4  
 5  width = 0
 6  height = 0
 7  coords = []
 8  for line in input.splitlines():
 9      x, y = map(int, line.split(", "))
10      coords.append((x, y))
11      width = max(width, x + 1)
12      height = max(height, y + 1)
13  
14  
15  def find_nearest(x, y):
16      distances = [abs(x - p) + abs(y - q) for p, q in coords]
17      best_distance = min(distances)
18      if distances.count(best_distance) > 1:
19          return None
20      return distances.index(best_distance)
21  
22  
23  counter = {}
24  edge = set()
25  for y in range(height):
26      for x in range(width):
27          nearest = find_nearest(x, y)
28          if nearest is not None:
29              counter[nearest] = counter.get(nearest, 0) + 1
30              if x in (0, width - 1) or y in (0, height - 1):
31                  edge.add(nearest)
32  print(max(counter[c] for c in counter if c not in edge))
33  width = 0
34  height = 0
35  coords = []
36  for line in input.splitlines():
37      x, y = map(int, line.split(", "))
38      coords.append((x, y))
39      width = max(width, x + 1)
40      height = max(height, y + 1)
41  
42  
43  def measure(x, y):
44      return sum(abs(x - p) + abs(y - q) for p, q in coords)
45  
46  
47  print(sum(measure(x, y) < 10000 for x in range(width) for y in range(height)))