-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_a.py
executable file
·194 lines (172 loc) · 5.44 KB
/
part_a.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env python3
import itertools
import math
import utils
class Challenge(utils.BaseChallenge):
def solve(self, _input, debug=False):
"""
>>> Challenge().default_solve()
288
"""
_, best_visibility = get_position_with_best_visibility(
parse_map(_input))
return best_visibility
def get_position_with_best_visibility(_map):
"""
>>> get_position_with_best_visibility(parse_map(\
".#..#\\n.....\\n#####\\n....#\\n...##\\n"))
((3, 4), 8)
>>> get_position_with_best_visibility(parse_map(\
"......#.#.\\n#..#.#....\\n..#######.\\n.#.#.###..\\n.#..#....."\
"\\n..#....#.#\\n#..#....#.\\n.##.#..###\\n##...#..#.\\n.#....####"))
((5, 8), 33)
>>> get_position_with_best_visibility(parse_map(\
"#.#...#.#.\\n.###....#.\\n.#....#...\\n##.#.#.#.#\\n....#.#.#."\
"\\n.##..###.#\\n..#...##..\\n..##....##\\n......#...\\n.####.###."))
((1, 2), 35)
>>> get_position_with_best_visibility(parse_map(\
".#..#..###\\n####.###.#\\n....###.#.\\n..###.##.#\\n##.##.#.#."\
"\\n....###..#\\n..#.#..#.#\\n#..#.#.###\\n.##...##.#\\n.....#.#.."))
((6, 3), 41)
>>> get_position_with_best_visibility(parse_map(\
".#..##.###...#######\\n##.############..##.\\n.#.######.########.#"\
"\\n.###.#######.####.#.\\n#####.##.#.##.###.##\\n..#####..#.#########"\
"\\n####################\\n#.####....###.#.#.##\\n##.#################"\
"\\n#####.##.###..####..\\n..######..##.#######\\n####.##.####...##..#"\
"\\n.#####..#.######.###\\n##...#.##########...\\n#.##########.#######"\
"\\n.####.#.###.###.#.##\\n....##.##.###..#####\\n.#.#.###########.###"\
"\\n#.#.#.#####.####.###\\n###.##.####.##.#..##"))
((11, 13), 210)
"""
visibility_mapping = get_visibility_mapping(_map)
best_position = max(
visibility_mapping, key=lambda point: visibility_mapping[point])
return best_position, visibility_mapping[best_position]
def get_visibility_mapping(_map):
"""
>>> get_visibility_mapping(parse_map(\
".#..#\\n.....\\n#####\\n....#\\n...##\\n"))
{(1, 0): 7, (4, 0): 7, (0, 2): 6, (1, 2): 7, (2, 2): 7, (3, 2): 7, \
(4, 2): 5, (4, 3): 7, (3, 4): 8, (4, 4): 7}
"""
return {
point: get_visible_asteroid_count(_map, point)
for point in _map
}
def get_visible_asteroid_count(_map, center):
"""
>>> get_visible_asteroid_count(parse_map(\
".#..#\\n.....\\n#####\\n....#\\n...##\\n"), (3, 4))
8
"""
return len(group_map_by_gcd(_map, center)) - 1
def group_map_by_gcd(_map, center):
"""
>>> group_map_by_gcd([(0, 0), (1, 1), (2, 2), (4, 2), (2, 1)], None)
{(0, 0): [(0, 0)], (1, 1): [(1, 1), (2, 2)], (2, 1): [(2, 1), (4, 2)]}
>>> group_map_by_gcd(parse_map(\
".#..#\\n.....\\n#####\\n....#\\n...##\\n"), (1, 2))
{(-1, 0): [(-1, 0)], (0, -1): [(0, -2)], (0, 0): [(0, 0)], \
(1, 0): [(1, 0), (2, 0), (3, 0)], (1, 1): [(2, 2)], (3, -2): [(3, -2)], \
(3, 1): [(3, 1)], (3, 2): [(3, 2)]}
"""
re_centered = re_center_map(_map, center)
return {
reduced: sorted(points, key=get_distance)
for reduced, points
in itertools.groupby(sorted(
re_centered, key=reduce_point), key=reduce_point)
}
def reduce_point(point):
"""
>>> reduce_point((0, 0))
(0, 0)
>>> reduce_point((0, 1))
(0, 1)
>>> reduce_point((0, -1))
(0, -1)
>>> reduce_point((1, 0))
(1, 0)
>>> reduce_point((-1, 0))
(-1, 0)
>>> reduce_point((1, 1))
(1, 1)
>>> reduce_point((1, 2))
(1, 2)
>>> reduce_point((2, 1))
(2, 1)
>>> reduce_point((2, 2))
(1, 1)
>>> reduce_point((6, 2))
(3, 1)
>>> reduce_point((12, 18))
(2, 3)
>>> reduce_point((-12, 18))
(-2, 3)
>>> reduce_point((12, -18))
(2, -3)
>>> reduce_point((-12, -18))
(-2, -3)
"""
if point == (0, 0):
return 0, 0
x, y = point
if x == 0:
return 0, int(y / abs(y))
if y == 0:
return int(x / abs(x)), 0
gcd = math.gcd(x, y)
return int(x / gcd), int(y / gcd)
def get_distance(point):
"""
>>> get_distance((0, 0))
0.0
>>> get_distance((3, 4))
5.0
>>> get_distance((-3, 4))
5.0
>>> get_distance((3, -4))
5.0
>>> get_distance((-3, -4))
5.0
"""
x, y = point
return math.sqrt(x * x + y * y)
def re_center_map(_map, center):
"""
>>> re_center_map([(1, 0), (4, 4)], None)
[(1, 0), (4, 4)]
>>> re_center_map([(1, 0), (4, 4)], (0, 0))
[(1, 0), (4, 4)]
>>> re_center_map([\
(1, 0), (4, 0), (0, 2), (1, 2), (2, 2), (3, 2), (4, 2), (4, 3),\
(3, 4), (4, 4)], (2, 2))
[(-1, -2), (2, -2), (-2, 0), (-1, 0), (0, 0), (1, 0), (2, 0), (2, 1), \
(1, 2), (2, 2)]
"""
if not center or center == (0, 0):
return _map
center_x, center_y = center
return [
(x - center_x, y - center_y)
for x, y in _map
]
def parse_map(map_text):
"""
>>> parse_map(".#..#\\n.....\\n#####\\n....#\\n...##\\n")
[(1, 0), (4, 0), (0, 2), (1, 2), (2, 2), (3, 2), (4, 2), (4, 3), (3, 4), \
(4, 4)]
"""
lines = map_text.splitlines()
non_empty_lines = filter(None, (
line.strip()
for line in lines
))
return [
(x, y)
for y, line in enumerate(non_empty_lines)
for x, section in enumerate(line)
if section == '#'
]
Challenge.main()
challenge = Challenge()