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.

day02-2.py 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import re
  2. from helpers import DEBUG, debug, load_input
  3. def split_line(line, character):
  4. return [group.strip() for group in line.split(character)]
  5. def main(red_max, blue_max, green_max):
  6. lines = load_input(day=2)
  7. games = []
  8. for line in lines:
  9. first_split = split_line(line, ":")
  10. game_num = int(re.match(r"Game (\d+)", first_split[0])[1])
  11. line_dict = {
  12. "game": game_num,
  13. "red": 0,
  14. "green": 0,
  15. "blue": 0
  16. }
  17. second_split = split_line(first_split[1], ";")
  18. for group in second_split:
  19. third_split = split_line(group, ",")
  20. for subgroup in third_split:
  21. matches = re.match(r"(\d+) ([a-z]+)", subgroup)
  22. color = matches[2]
  23. number = int(matches[1])
  24. if number > line_dict[color]:
  25. line_dict[color] = number
  26. games.append(line_dict)
  27. games_power = 0
  28. for game in games:
  29. games_power += (game["red"] * game["blue"] * game["green"])
  30. print(games_power)
  31. if __name__ == "__main__":
  32. main(red_max=12, blue_max=14, green_max=13)