123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- from functools import reduce
-
- from helpers import Helper
-
- helper = Helper(debug=True)
- debug = helper.debug
- load_input = helper.load_input
-
- def main():
- lines = load_input(6)
-
- races = []
-
- times = []
- distances = []
- for line in lines:
- header, values_str = line.split(":")
- for value in values_str.split():
- if header == "Time":
- times.append(int(value))
- else:
- distances.append(int(value))
-
- for item in zip(times, distances):
- races.append({"Time": item[0], "Distance": item[1]})
-
- # debug(races)
-
- ways_to_win = []
- for i, race in enumerate(races):
- win_ways = 0
- for held_time in range(race["Time"]):
- run_time = race["Time"] - held_time
- run_distance = run_time * held_time
- if run_distance > race["Distance"]:
- debug(f"I can win race {i} with time {held_time}ms (it'll run {run_time}ms at {held_time}mm/ms for {run_distance}mm total.)")
- win_ways += 1
- ways_to_win.append(win_ways)
-
- total_ways_to_win = reduce(lambda x,y: x*y, ways_to_win)
-
- print(f"Total ways to win all races: {total_ways_to_win} ({'*'.join([str(i) for i in ways_to_win])})")
-
- if __name__ == "__main__":
- main()
|