-
Notifications
You must be signed in to change notification settings - Fork 0
/
dec6_1.py
76 lines (65 loc) · 2.21 KB
/
dec6_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""This program solves the Advent of Code challenge for December 6 part 1"""
def read_data():
"""This function read in the data and returns the coordinates,
the x range and the y range"""
#input_file = open("input_data/dec6.txt", 'r')
input_file = open("../../Downloads/day6.txt", 'r')
coordinates = []
min_x = None
max_x = None
min_y = None
max_y = None
first = True
for line in input_file:
values = line.split(', ')
x_val = int(values[0])
y_val = int(values[1])
coordinates.append((x_val, y_val))
if first:
min_x = x_val
max_x = x_val
min_y = y_val
max_y = y_val
first = False
else:
if x_val < min_x:
min_x = x_val
if x_val > max_x:
max_x = x_val
if y_val < min_y:
min_y = y_val
if y_val > max_y:
max_y = y_val
input_file.close()
return coordinates, (min_x, max_x), (min_y, max_y)
def largest_finite_area():
"""This function finds and returns the largest finite
area associated with a coordinate"""
coordinates, x_range, y_range = read_data()
total_areas = [0]*len(coordinates)
grid = []
for i in range(x_range[1]-x_range[0]):
column = []
for j in range(y_range[1]-y_range[0]):
min_distance = x_range[1]-x_range[0]+y_range[1]-y_range[0]
min_distance_label = -1
for k, item in enumerate(coordinates):
distance = abs(item[0]-i-x_range[0])+abs(item[1]-j-y_range[0])
if distance < min_distance:
min_distance = distance
min_distance_label = k
column.append(min_distance_label)
total_areas[min_distance_label] += 1
grid.append(column)
for i in [0, -1]:
for j in range(y_range[1]-y_range[0]):
total_areas[grid[i][j]] = 0
for j in [0, -1]:
for i in range(x_range[1]-x_range[0]):
total_areas[grid[i][j]] = 0
max_area = 0
for val in total_areas:
if val > max_area:
max_area = val
return max_area
print(largest_finite_area())