with open("input.txt") as f: lines = f.readlines() times, distances = lines times = [int(t) for t in times.split()[1:]] distances = [int(d) for d in distances.split()[1:]] races = zip(times, distances) def distance(t, t_max): """ Calculate the distance traveled if you charge for t seconds """ return (t) * (t_max - t) def part1(races): answer = 1 for race in races: t_max, d_max = race results = [ (t, distance(t, t_max)) for t in range(t_max) if distance(t, t_max) > d_max ] answer *= len(results) return answer print(part1(races)) def part2(times, distances): t_max = int("".join(str(t) for t in times)) d_max = int("".join(str(d) for d in distances)) for t in range(t_max): if distance(t, t_max) > d_max: break return t_max - 2*t + 1 print(part2(times, distances))