-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday10.py
More file actions
115 lines (105 loc) · 3.92 KB
/
day10.py
File metadata and controls
115 lines (105 loc) · 3.92 KB
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
from fractions import Fraction
import numpy as np
with open('/Users/relyea/data/input.txt') as input_file:
inpstring = input_file.readlines()
inpstring = [aa.strip() for aa in inpstring]
asteroid_list = []
for ii in range(len(inpstring[0])):
for jj in range(len(inpstring)):
if inpstring[jj][ii] == '#':
asteroid_list.append((ii,jj))
pairlist = {}
for aa in asteroid_list:
pairlist[aa] = []
for ii in range(len(asteroid_list)-1):
for jj in range(ii+1,len(asteroid_list)):
asteroid = asteroid_list[ii]
otherasteroid = asteroid_list[jj]
xstep = otherasteroid[0] - asteroid[0]
ystep = otherasteroid[1] - asteroid[1]
if xstep == 0:
pair = True
for yy in range(asteroid[1]+1,otherasteroid[1]):
if (asteroid[0], yy) in asteroid_list:
pair = False
break
if pair == True:
pairlist[asteroid].append(otherasteroid)
pairlist[otherasteroid].append(asteroid)
elif ystep == 0:
pair = True
for xx in range(asteroid[0]+1,otherasteroid[0]):
if (xx, asteroid[1]) in asteroid_list:
pair = False
break
if pair == True:
pairlist[asteroid].append(otherasteroid)
pairlist[otherasteroid].append(asteroid)
else:
thefraction = Fraction(xstep, ystep)
x_it = np.abs(thefraction.numerator)*np.sign(xstep)
y_it = np.abs(thefraction.denominator)*np.sign(ystep)
nsteps = int(abs(xstep) / abs(x_it))
pair = True
for istep in range(1,nsteps):
if (asteroid[0] + istep*x_it, asteroid[1] + istep*y_it) in asteroid_list:
pair = False
break
if pair == True:
pairlist[asteroid].append(otherasteroid)
pairlist[otherasteroid].append(asteroid)
maxnum = 0
for pair in pairlist:
# print(pair, len(pairlist[pair]))
if len(pairlist[pair]) > maxnum:
maxnum = len(pairlist[pair])
print(pair)
asteroid_list = copy(orig_asteroid_list)
coords = (23,19)
index = asteroid_list.index(coords)
newpairlist = []
for ii in range(len(asteroid_list)):
asteroid = coords
otherasteroid = asteroid_list[ii]
if otherasteroid == asteroid:
continue
xstep = otherasteroid[0] - asteroid[0]
ystep = otherasteroid[1] - asteroid[1]
if xstep == 0:
y_it = np.sign(ystep)
nsteps = int(abs(ystep) / abs(y_it))
pair = True
for istep in range(1,nsteps):
if (asteroid[0], asteroid[1] + istep*y_it) in asteroid_list:
pair = False
break
if pair == True:
newpairlist.append(otherasteroid)
elif ystep == 0:
x_it = np.sign(xstep)
nsteps = int(abs(xstep) / abs(x_it))
pair = True
for istep in range(1,nsteps):
if (asteroid[0] + istep*x_it, asteroid[1]) in asteroid_list:
pair = False
break
if pair == True:
newpairlist.append(otherasteroid)
else:
thefraction = Fraction(xstep, ystep)
x_it = np.abs(thefraction.numerator)*np.sign(xstep)
y_it = np.abs(thefraction.denominator)*np.sign(ystep)
nsteps = int(abs(xstep) / abs(x_it))
pair = True
for istep in range(1,nsteps):
if (asteroid[0] + istep*x_it, asteroid[1] + istep*y_it) in asteroid_list:
pair = False
break
if pair == True:
newpairlist.append(otherasteroid)
pair_to_angle = []
for newasteroid in newpairlist:
theangle = np.arctan2(newasteroid[0] - asteroid[0], newasteroid[1] - asteroid[1])
pair_to_angle.append([theangle, newasteroid[0], newasteroid[1]])
pta = np.array(pair_to_angle)
indices = np.argsort(-pta[:,0])