-
Notifications
You must be signed in to change notification settings - Fork 2
/
sol.py
53 lines (46 loc) · 1.27 KB
/
sol.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
from threading import Thread
from queue import Queue
from computer import Computer
program = list(map(int, open('../input.txt').read().split(',')))
def get_coords(x, y, program):
queue_out = Queue()
queue_in = Queue()
c = Computer(program.copy(), queue_in, queue_out)
Thread(target=c.run).start()
queue_in.put(x)
queue_in.put(y)
return queue_out.get()
def find_max_y(x, program):
y, Y = 0, x/0.6
while True:
m = (y+Y)//2
if get_coords(x, m, program):
Y = m
else:
y = m
if y == Y-1:
return Y
def is_good(x, y, program):
return get_coords(x, y, program) and \
get_coords(x, y+99, program) and \
get_coords(x-99, y+99, program) and \
get_coords(x-99, y, program)
def part_one(program):
ans = 0
for x in range(50):
for y in range(50):
ans += get_coords(x, y, program)
return ans
def part_two(program):
x, X = 0, 10000
while True:
m = (x+X)//2
y = find_max_y(m, program)
if is_good(m, y, program):
X = m
else:
x = m
if x == X-1:
return (X-99)*10000+find_max_y(X, program)
print(f"Part one: {part_one(program)}")
print(f"Part two: {part_two(program)}")