1+ #!/usr/bin/env python
2+ # -*- coding: utf-8 -*-
3+
4+ split_data = True
5+ completed = True
6+ raw_data = None # Not To be touched
7+
8+ def part1 (data ):
9+ minX ,minY ,maxX ,maxY = float ('inf' ),float ('inf' ),float ('-inf' ),float ('-inf' )
10+ points = []
11+ area = {}
12+ for line in data :
13+ x , y = line .split (',' )
14+ x , y = int (x ), int (y )
15+ points .append ((x , y ))
16+ area [(x ,y )] = 0
17+ minX , minY , maxX , maxY = min (minX , x - 1 ), min (minY , y - 1 ), max (maxX , x + 1 ), max (maxY , y + 1 )
18+
19+ black_listed = set ()
20+ # We can determine the inf ones if they touch the boundary points
21+ # We will keep track of the area...
22+
23+ for x in range (minX , maxX + 1 ):
24+ for y in range (minY , maxY + 1 ):
25+ # Finding the closest point
26+ double_best = False
27+ best_point = None
28+ best_d = float ('inf' )
29+ for point in points :
30+ px , py = point
31+ dis = abs (px - x ) + abs (py - y )
32+ if dis < best_d :
33+ double_best = False
34+ best_point = point
35+ best_d = dis
36+ elif dis == best_d :
37+ double_best = True
38+
39+ # Now we deal with adding areas and blacklisting
40+ if double_best != True :
41+ area [best_point ] += 1
42+ if x in (minX , maxX ) or y in (minY , maxY ): # We are dealing with a boarder point
43+ black_listed .add (best_point )
44+
45+ # Now we find point with the greatest area
46+ best_area = 0
47+ for point in points :
48+ if point in black_listed : continue
49+ if area [point ] > best_area :
50+ best_area = area [point ]
51+
52+ return best_area
53+
54+
55+ def part2 (data ):
56+ minX ,minY ,maxX ,maxY = float ('inf' ),float ('inf' ),float ('-inf' ),float ('-inf' )
57+ points = []
58+ for line in data :
59+ x , y = line .split (',' )
60+ x , y = int (x ), int (y )
61+ points .append ((x , y ))
62+ minX , minY , maxX , maxY = min (minX , x - 1 ), min (minY , y - 1 ), max (maxX , x + 1 ), max (maxY , y + 1 )
63+
64+ # We can determine the inf ones if they touch the boundary points
65+ # We will keep track of the area...
66+
67+ region_area = 0
68+ for x in range (minX , maxX + 1 ):
69+ for y in range (minY , maxY + 1 ):
70+ # Finding the total cost
71+ cost = 0
72+ for point in points :
73+ px , py = point
74+ dis = abs (px - x ) + abs (py - y )
75+ cost += dis
76+
77+ if cost < 10000 :
78+ region_area += 1
79+
80+ return region_area
0 commit comments