67 lines
1.7 KiB
Python
67 lines
1.7 KiB
Python
|
from lib import str_to_ints
|
||
|
|
||
|
|
||
|
def part_1(data):
|
||
|
counts = {}
|
||
|
x_min, x_max, y_min, y_max = 10**9, 0, 10**9, 0
|
||
|
for line in data.splitlines():
|
||
|
x, y = str_to_ints(line)
|
||
|
x_min = min(x_min, x)
|
||
|
x_max = max(x_max, x)
|
||
|
y_min = min(y_min, y)
|
||
|
y_max = max(y_max, y)
|
||
|
counts[(x, y)] = 0
|
||
|
|
||
|
infs = set()
|
||
|
for x in range(x_min, x_max + 1):
|
||
|
for y in range(y_min, y_max + 1):
|
||
|
if (x, y) in counts:
|
||
|
counts[(x, y)] += 1
|
||
|
continue
|
||
|
|
||
|
min_dist = 10**9
|
||
|
min_fields = []
|
||
|
for (nx, ny) in counts.keys():
|
||
|
d = abs(nx - x) + abs(ny - y)
|
||
|
if d == min_dist:
|
||
|
min_fields.append((nx, ny))
|
||
|
elif d < min_dist:
|
||
|
min_dist = d
|
||
|
min_fields = [(nx, ny)]
|
||
|
|
||
|
if len(min_fields) == 1:
|
||
|
(nx, ny), = min_fields
|
||
|
if x == x_min or y == y_min or x == x_max or y == y_max:
|
||
|
infs.add((nx, ny))
|
||
|
else:
|
||
|
counts[(nx, ny)] += 1
|
||
|
for c in infs:
|
||
|
del counts[c]
|
||
|
print(max(list(counts.values())))
|
||
|
|
||
|
|
||
|
def part_2(data):
|
||
|
coords = []
|
||
|
for line in data.splitlines():
|
||
|
coords.append(str_to_ints(line))
|
||
|
|
||
|
r = 0
|
||
|
for x in range(-500, 500):
|
||
|
for y in range(-500, 500):
|
||
|
d = sum([abs(nx - x) + abs(ny - y) for (nx, ny) in coords])
|
||
|
if d < 10_000:
|
||
|
r += 1
|
||
|
print(r)
|
||
|
|
||
|
|
||
|
def main():
|
||
|
input_file = __file__.replace(".py", ".txt")
|
||
|
with open(input_file) as f:
|
||
|
data = f.read()
|
||
|
part_1(data)
|
||
|
part_2(data)
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|