Advent of Code 2022 https://adventofcode.com/2022
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.

day2-2.py 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # https://adventofcode.com/2022/day/2
  2. MOVE_SCORES = {
  3. "A": 1,
  4. "B": 2,
  5. "C": 3,
  6. "X": 0,
  7. "Y": 3,
  8. "Z": 6
  9. }
  10. MOVE_BEATS = {
  11. "A": "C",
  12. "B": "A",
  13. "C": "B"
  14. }
  15. MOVE_LOSES = {
  16. "A": "B",
  17. "B": "C",
  18. "C": "A"
  19. }
  20. def main():
  21. with open("input2.txt", "r") as file:
  22. moves = [line.strip() for line in file.readlines()]
  23. # moves = [(x, y) for line in inlines for x, y in line.split(" ")]
  24. scores = []
  25. for move in moves:
  26. (theirs, mine) = move.split(" ")
  27. cur_score = MOVE_SCORES[mine]
  28. if mine == "X":
  29. # Have to lose.
  30. # Add the score of the move that their move beats.
  31. cur_score += MOVE_SCORES[MOVE_BEATS[theirs]]
  32. elif mine == "Y":
  33. # Have to tie.
  34. # Add the score of the move that they made.
  35. cur_score += MOVE_SCORES[theirs]
  36. elif mine == "Z":
  37. # Have to win.
  38. # Add the score of the move that theirs loses to.
  39. cur_score += MOVE_SCORES[MOVE_LOSES[theirs]]
  40. scores.append(cur_score)
  41. print(sum(scores))
  42. if __name__ == "__main__":
  43. main()