Let's see how far I get this year.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

day02-1.py 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. matching_games = 0
  31. for game in games:
  32. if game["red"] <= red_max and game["green"] <= green_max and game["blue"] <= blue_max:
  33. matching_games += game["game"]
  34. print(matching_games)
  35. if __name__ == "__main__":
  36. main(red_max=12, blue_max=14, green_max=13)