58 lines
1.4 KiB
Python
58 lines
1.4 KiB
Python
EXAMPLE = """
|
|
Time: 7 15 30
|
|
Distance: 9 40 200
|
|
"""
|
|
|
|
def str_to_int_lst(s):
|
|
return list(map(int, s.split()))
|
|
|
|
def clean(text: str) -> list[str]:
|
|
return list(filter(lambda l: l.strip() != "", text.splitlines()))
|
|
|
|
def solve(lines: list[str]):
|
|
res = 1
|
|
t = []
|
|
for (_, line) in enumerate(lines):
|
|
numbers = str_to_int_lst(line.replace("Time:", "").replace("Distance:", "").strip())
|
|
t.append(numbers)
|
|
|
|
t = list(zip(*t))
|
|
for time, dist in t:
|
|
opt = 0
|
|
for press_time in range(1, time):
|
|
dist_trav = press_time * (time - press_time)
|
|
if dist_trav > dist:
|
|
opt += 1
|
|
res *= opt
|
|
return res
|
|
|
|
def solve2(lines: list[str]):
|
|
t = []
|
|
for (_, line) in enumerate(lines):
|
|
numbers = str_to_int_lst(line.replace("Time:", "").replace("Distance:", "").replace(" ", "").strip())
|
|
t.append(numbers[0])
|
|
time, dist = t
|
|
opt = 0
|
|
for press_time in range(1, time):
|
|
dist_trav = press_time * (time - press_time)
|
|
if dist_trav > dist:
|
|
opt += 1
|
|
return opt
|
|
|
|
def main():
|
|
example = clean(EXAMPLE)
|
|
print("Example 1:", solve(example))
|
|
|
|
data = clean(open("i6.txt").read())
|
|
print("Solution 1:", solve(data))
|
|
|
|
example = clean(EXAMPLE)
|
|
print("Example 2:", solve2(example))
|
|
|
|
data = clean(open("i6.txt").read())
|
|
print("Solution 2:", solve2(data))
|
|
return
|
|
|
|
if __name__ == "__main__":
|
|
main()
|