-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
61 lines (46 loc) · 1.94 KB
/
solver.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
import sys
import re
class Solver:
def __init__(self, file_path = "test.txt"):
# read input file
with open(file_path) as f:
self.lines = f.readlines()
number_expression = r"(\d+)"
times = re.findall(number_expression, self.lines[0])
distances = re.findall(number_expression, self.lines[1])
self.races = list(zip(map(int, times), map(int, distances)))
self.single_race = (int("".join(times)), int("".join(distances)))
def part1(self):
total = 1
for race_time, race_record in self.races:
total_race = 0
# find everytime that the distance is greater than the record
for button in range(race_record):
time_left = race_time - button
distance = button * (time_left)
if distance > race_record:
# distance is greater than the record
total_race += 1
total *= total_race
return total
# this is the same as part1, but with a different way to calculate the total
def part2(self):
total = 0
race_time, race_record = self.single_race
# find the first time that the distance is greater than the record
for button in range(race_record):
time_left = race_time - button
distance = button * (time_left)
if distance > race_record:
# the first time that the distance is greater than the record
break
total = race_time - button*2 + 1
return total
if __name__ == '__main__':
if len(sys.argv) > 1 and 1 <= int(sys.argv[1]) <= 2:
if sys.argv[1] == '1':
solver = Solver("input1.txt")
print(solver.part1())
else:
solver = Solver("input1.txt")
print(solver.part2())