-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path10.py
134 lines (99 loc) Β· 3 KB
/
10.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
#!/usr/bin/env python
import math
from collections import defaultdict
from itertools import product
from operator import itemgetter
with open(__file__, "r") as f:
c = f.read()
space = list(zip(*c[c.rindex("π
") + 1 : c.rindex("π")].rstrip().split("\n")))
def get_count(p, q):
if p[0] == q[0]:
return abs(p[1] - q[1])
if p[1] == q[1]:
return abs(p[0] - q[0])
return math.gcd(abs(p[0] - q[0]), abs(p[1] - q[1]))
d = defaultdict(int)
side = len(space[0])
for p in product(range(side), repeat=2):
if space[p[0]][p[1]] != "#":
continue
for q in product(range(side), repeat=2):
if space[q[0]][q[1]] != "#" or (p[0] == q[0] and p[1] == q[1]):
continue
c = get_count(p, q)
dx = (q[0] - p[0]) // c
dy = (q[1] - p[1]) // c
d[p] += 1 == sum(
space[p[0] + dx * i][p[1] + dy * i] == "#" for i in range(1, c + 1)
)
part_one = max(d.values())
print(part_one)
assert part_one == 282
sx, sy = max(d.items(), key=itemgetter(1))[0]
refvec = (0, 1)
# https://stackoverflow.com/questions/41855695
def clockwise(vector):
length = math.hypot(vector[0], vector[1])
if length == 0:
return -math.pi, 0
normalized = (vector[0] / length, vector[1] / length)
dotprod = normalized[0] * refvec[0] + normalized[1] * refvec[1]
diffprod = refvec[1] * normalized[0] - refvec[0] * normalized[1]
angle = math.atan2(diffprod, dotprod)
if angle < 0:
return 2 * math.pi + angle, length
return angle, length
grid = map(lambda p: (p[0] - sx, -(p[1] - sy)), product(range(side), repeat=2))
sorted_positions = sorted(grid, key=clockwise)[1:]
def remap(p):
x, y = p
return (x + sx, -y + sy)
asteroids = [p for p in sorted_positions if space[remap(p)[0]][remap(p)[1]] == "#"]
counter = 0
last_angle = 0
i = 0
while asteroids:
if i > len(asteroids) - 1:
i = 0
x, y = asteroids[i]
a = math.atan2(y, x)
if last_angle == a:
i += 1
continue
pt = asteroids.pop(i)
counter += 1
last_angle = a
if counter == 200:
part_two = 100 * remap(pt)[0] + remap(pt)[1]
break
print(part_two)
assert part_two == 1008
"""π
###..#.##.####.##..###.#.#..
#..#..###..#.......####.....
#.###.#.##..###.##..#.###.#.
..#.##..##...#.#.###.##.####
.#.##..####...####.###.##...
##...###.#.##.##..###..#..#.
.##..###...#....###.....##.#
#..##...#..#.##..####.....#.
.#..#.######.#..#..####....#
#.##.##......#..#..####.##..
##...#....#.#.##.#..#...##.#
##.####.###...#.##........##
......##.....#.###.##.#.#..#
.###..#####.#..#...#...#.###
..##.###..##.#.##.#.##......
......##.#.#....#..##.#.####
...##..#.#.#.....##.###...##
.#.#..#.#....##..##.#..#.#..
...#..###..##.####.#...#..##
#.#......#.#..##..#...#.#..#
..#.##.#......#.##...#..#.##
#.##..#....#...#.##..#..#..#
#..#.#.#.##..#..#.#.#...##..
.#...#.........#..#....#.#.#
..####.#..#..##.####.#.##.##
.#.######......##..#.#.##.#.
.#....####....###.#.#.#.####
....####...##.#.#...#..#.##.
π"""