Let's see how far I get this year.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

day04-1.py 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from helpers import Helper
  2. helper = Helper(debug=True)
  3. load_input = helper.load_input
  4. debug = helper.debug
  5. class Card:
  6. """ A single scratch-off card for AOC 2023 day 4. """
  7. def __init__(self, input_line):
  8. card_id, numbers = input_line.split(": ")
  9. winning, have = numbers.split(" | ")
  10. self.idnum = card_id.split()[1]
  11. self.winning = [int(n) for n in winning.split()]
  12. self.have = [int(n) for n in have.split()]
  13. @property
  14. def num_winners(self) -> int:
  15. winners = 0
  16. for number in self.have:
  17. if number in self.winning:
  18. winners += 1
  19. return winners
  20. @property
  21. def score(self) -> int:
  22. return 2**(self.num_winners-1) if self.num_winners > 0 else 0
  23. def main():
  24. lines = load_input(4)
  25. cards = []
  26. for line in lines:
  27. cards.append(Card(input_line=line))
  28. total_score = 0
  29. for card in cards:
  30. total_score += card.score
  31. print(f"Total score: {total_score}")
  32. if __name__ == "__main__":
  33. main()