-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6.py
53 lines (43 loc) · 1.57 KB
/
day6.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
#!/usr/bin/env python3
import numpy as np
f = open("ressources/day6.txt", "r") #Open File
lines = f.readlines() #Separate in lines
#Q1
times = list(map(int, lines[0].split()[1::]))
distances = list(map(int, lines[1].split()[1::]))
ways = []
for time, distance in zip(times, distances):
endDistance = np.multiply(list(range(0, time, 1)), list(range(time, 0, -1)))
ways.append(np.sum(endDistance > distance))
print("Ways to beat all the records is: {}".format(np.prod(ways)))
#Q2
time = int(''.join(lines[0].split()[1::]))
distance = int(''.join(lines[1].split()[1::]))
#BruteForce
# endDistance = np.multiply(list(range(0, time, 1)), list(range(time, 0, -1)))
# nbWays = np.sum(endDistance > distance)
#SmoothForce by dicho
def dichoMin(time, distance, minTime, maxTime):
while minTime < maxTime:
middleTime = (minTime + maxTime) // 2
if (time - middleTime) * middleTime > distance:
minTime = minTime
maxTime = middleTime
else:
minTime = middleTime + 1
maxTime = maxTime
return minTime
def dichoMax(time, distance, minTime, maxTime):
while minTime < maxTime:
middleTime = (minTime + maxTime) // 2
if (time - middleTime) * middleTime > distance:
minTime = middleTime + 1
maxTime = maxTime
else:
minTime = minTime
maxTime = middleTime
return minTime
minTime = dichoMin(time, distance, 1, time//2+1)
maxTime = dichoMax(time, distance, time//2-1, time-1)
nbWays = maxTime - minTime;
print("Ways to beat the race is: {}".format(nbWays))