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.2KB

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