-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday10.py
executable file
·151 lines (104 loc) · 2.39 KB
/
day10.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
#!/usr/bin/env python3
from utils.all import *
from fractions import Fraction
fin = advent.get_input()
# eprint(*fin, sep='')
timer_start()
##################################################
charmat = read_char_matrix(fin)
def frac(x1, y1, x2, y2):
if x1 == x2:
return 'inf', -1 if y1 > y2 else 1
f = Fraction(y2 - y1, x2 - x1)
if f == 0:
return f, -1 if x1 > x2 else 1
return f, -1 if y1 > y2 else 1
def dist2(x1, y1, x2, y2):
return (x2-x1)**2 + (y2-y1)**2
ast = set()
for y in range(len(charmat)):
for x in range(len(charmat[y])):
if charmat[y][x] != '.':
ast.add((x, y))
in_sight = defaultdict(int)
# aligned = defaultdict(set)
set_in_sight = defaultdict(dict)
# print('total', len(ast))
for i, a in enumerate(ast):
# print(i)
los = set()
for b in ast:
if a == b:
continue
m = frac(*a, *b)
# aligned[m].add(b)
d = dist2(*a, *b)
if m in set_in_sight[a]:
prevd = dist2(*a, *set_in_sight[a][m])
if prevd > d:
set_in_sight[a][m] = b
else:
set_in_sight[a][m] = b
if m not in los:
in_sight[a] += 1
los.add(m)
best = max(in_sight, key=lambda k: in_sight[k])
# print(best, in_sight[best])
# 712 wrong
# 285 too low
# 286 too low
advent.submit_answer(1, in_sight[best])
from math import atan2, pi
import cmath
def key_angle(src):
sx, sy = src
def k(a):
ax, ay = a
x = ax-sx
y = ay-sy
rad = atan2(y, x)
deg = (rad if rad > 0 else (2*pi + rad)) * 360 / (2*pi)
if deg == 360:
deg = 0
deg += 90.0
if deg > 360:
deg = deg - 360
if deg == 360:
deg = 0
return deg # hey, it if works it works...
return k
keyfunc = key_angle(best)
target = 200
while True:
in_sight = defaultdict(int)
set_in_sight = defaultdict(dict)
for i, a in enumerate(ast):
# print(i)
los = set()
for b in ast:
if a == b:
continue
m = frac(*a, *b)
# aligned[m].add(b)
d = dist2(*a, *b)
if m in set_in_sight[a]:
prevd = dist2(*a, *set_in_sight[a][m])
if prevd > d:
set_in_sight[a][m] = b
else:
set_in_sight[a][m] = b
if m not in los:
in_sight[a] += 1
los.add(m)
to_kill = len(set_in_sight[best])
assert to_kill != 0, 'wtf'
if to_kill < target:
target -= to_kill
else:
ordered = sorted(set_in_sight[best].values(), key=keyfunc)
# for o in ordered:
# print(keyfunc(o), o)
chosen = ordered[target - 1]
break
# print('>', chosen)
advent.submit_answer(2, chosen[0] * 100 + chosen[1])