Guest User

Untitled

a guest
Dec 3rd, 2022
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """
  3. Rock defeats Scissors, Scissors defeats Paper, and Paper defeats Rock.
  4. A for Rock, B for Paper, and C for Scissors
  5. X for Rock, Y for Paper, and Z for Scissors
  6. 1 for Rock, 2 for Paper, and 3 for Scissors
  7. 0 if you lost, 3 if the round was a draw, and 6 if you won
  8. Second part:
  9. X means you need to lose,
  10. Y means you need to draw,
  11. Z means you need to win
  12. """
  13.  
  14. import sys
  15. from pathlib import Path
  16.  
  17. INPUTS_DIR = 'inputs'
  18. INPUT_FILE = 'day02.txt'
  19. PROJECT_DIR = Path('.')
  20. FILE_PATH = PROJECT_DIR / INPUTS_DIR / INPUT_FILE
  21.  
  22. def read_data(file_path):
  23.     for row in open(file_path):
  24.         yield row.split()
  25.  
  26.  
  27. def sum_result(file_path):
  28.     A = X = 1
  29.     B = Y = 2
  30.     C = Z = 3
  31.     scores = {
  32.         ('A', 'X'): 3,
  33.         ('A', 'Y'): 6,
  34.         ('A', 'Z'): 0,
  35.         ('B', 'X'): 0,
  36.         ('B', 'Z'): 6,
  37.         ('B', 'Y'): 3,
  38.         ('C', 'X'): 6,
  39.         ('C', 'Y'): 0,
  40.         ('C', 'Z'): 3
  41.     }
  42.     results = {
  43.         'X': {t[0]: t[1] for t in scores if scores[t] == 0},
  44.         'Y': {t[0]: t[1] for t in scores if scores[t] == 3},
  45.         'Z': {t[0]: t[1] for t in scores if scores[t] == 6},
  46.     }
  47.     sum_tmp_1 = 0
  48.     sum_tmp_2 = 0
  49.     _vars = vars().copy()
  50.     for x, y in read_data(file_path):
  51.         sum_tmp_1 += (
  52.             scores[(x, y)] + _vars[y]
  53.             )
  54.         z = results.get(y).get(x)
  55.         sum_tmp_2 += _vars[z] + scores[(x, z)]
  56.     return sum_tmp_1, sum_tmp_2
  57.  
  58.  
  59. if __name__ == '__main__':
  60.     FILE_PATH = sys.argv[1] if len(sys.argv) > 1 else FILE_PATH
  61.     print(sum_result(FILE_PATH))
  62.  
Advertisement
Add Comment
Please, Sign In to add comment