solution.py 1023 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/python3
  2. import sys
  3. # part 1
  4. # A/X rock, B/Y paper, C/Z scissors
  5. win = { 'A': 'Y', 'B': 'Z', 'C': 'X', }
  6. draw = { 'A': 'X', 'B': 'Y', 'C': 'Z', }
  7. loss = { 'A': 'Z', 'B': 'X', 'C': 'Y', }
  8. scores = { 'X': 1, 'Y': 2, "Z": 3, }
  9. def calculate_points(col1, col2):
  10. score = scores[col2] + 6 if win[col1] == col2 else \
  11. scores[col2] + 3 if draw[col1] == col2 else \
  12. scores[col2] + 0
  13. return score
  14. def part1():
  15. total = 0
  16. for line in sys.stdin:
  17. col1, col2 = line.strip().split(' ')
  18. total += calculate_points(col1, col2)
  19. print(f'total score of {total} points')
  20. # part 2
  21. mapping = { 'X': loss, 'Y': draw, 'Z': win }
  22. def part2():
  23. total = 0
  24. for line in sys.stdin:
  25. prediction, strategy = line.strip().split(' ')
  26. move = mapping[strategy][prediction]
  27. #print(prediction, strategy, move)
  28. total += calculate_points(prediction, move)
  29. print(f'total score of {total} points')
  30. if sys.argv[1] in '1':
  31. part1()
  32. else:
  33. part2()