Let's see how far I get this year.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

day06-1.py 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from functools import reduce
  2. from helpers import Helper
  3. helper = Helper(debug=True)
  4. debug = helper.debug
  5. load_input = helper.load_input
  6. def main():
  7. lines = load_input(6)
  8. races = []
  9. times = []
  10. distances = []
  11. for line in lines:
  12. header, values_str = line.split(":")
  13. for value in values_str.split():
  14. if header == "Time":
  15. times.append(int(value))
  16. else:
  17. distances.append(int(value))
  18. for item in zip(times, distances):
  19. races.append({"Time": item[0], "Distance": item[1]})
  20. # debug(races)
  21. ways_to_win = []
  22. for i, race in enumerate(races):
  23. win_ways = 0
  24. for held_time in range(race["Time"]):
  25. run_time = race["Time"] - held_time
  26. run_distance = run_time * held_time
  27. if run_distance > race["Distance"]:
  28. 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.)")
  29. win_ways += 1
  30. ways_to_win.append(win_ways)
  31. total_ways_to_win = reduce(lambda x,y: x*y, ways_to_win)
  32. print(f"Total ways to win all races: {total_ways_to_win} ({'*'.join([str(i) for i in ways_to_win])})")
  33. if __name__ == "__main__":
  34. main()