-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday6_wait_for_it.py
More file actions
82 lines (69 loc) · 2.53 KB
/
day6_wait_for_it.py
File metadata and controls
82 lines (69 loc) · 2.53 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
class Race:
def __init__(self, time, distance):
self.time = time
self.distance = distance
def __repr__(self):
return self.__dict__
part1_simple_races = [
Race(time=7, distance=9),
Race(time=15, distance=40),
Race(time=30, distance=200),
]
part1_races = [
Race(time=41, distance=214),
Race(time=96, distance=1789),
Race(time=88, distance=1127),
Race(time=94, distance=1055),
]
part2_simple_race = Race(time=71530, distance=940200)
part2_race = Race(time=41968894, distance=214178911271055)
def calculate_distance_travelled(hold_time: int, race_time_limit: int) -> int:
return (race_time_limit - hold_time) * hold_time
def part1():
error_margins = []
for race in part1_races:
shortest_hold_time = 0
longest_hold_time = race.time
short_found = False
long_found = False
while shortest_hold_time < longest_hold_time and not (short_found and long_found):
if not short_found:
short_hold_distance = calculate_distance_travelled(shortest_hold_time, race.time)
if short_hold_distance > race.distance:
short_found = True
else:
shortest_hold_time += 1
if not long_found:
long_hold_distance = calculate_distance_travelled(longest_hold_time, race.time)
if long_hold_distance > race.distance:
long_found = True
else:
longest_hold_time -= 1
margin = longest_hold_time - shortest_hold_time + 1
error_margins.append(margin)
res = 1
for x in error_margins:
res *= x
return res
def part2():
shortest_hold_time = 0
longest_hold_time = part2_race.time
short_found = False
long_found = False
while shortest_hold_time < longest_hold_time and not (short_found and long_found):
if not short_found:
short_hold_distance = calculate_distance_travelled(shortest_hold_time, part2_race.time)
if short_hold_distance > part2_race.distance:
short_found = True
else:
shortest_hold_time += 1
if not long_found:
long_hold_distance = calculate_distance_travelled(longest_hold_time, part2_race.time)
if long_hold_distance > part2_race.distance:
long_found = True
else:
longest_hold_time -= 1
margin = longest_hold_time - shortest_hold_time + 1
return margin
print(part1())
print(part2())