Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- """
- Rock defeats Scissors, Scissors defeats Paper, and Paper defeats Rock.
- A for Rock, B for Paper, and C for Scissors
- X for Rock, Y for Paper, and Z for Scissors
- 1 for Rock, 2 for Paper, and 3 for Scissors
- 0 if you lost, 3 if the round was a draw, and 6 if you won
- Second part:
- X means you need to lose,
- Y means you need to draw,
- Z means you need to win
- """
- import sys
- from pathlib import Path
- INPUTS_DIR = 'inputs'
- INPUT_FILE = 'day02.txt'
- PROJECT_DIR = Path('.')
- FILE_PATH = PROJECT_DIR / INPUTS_DIR / INPUT_FILE
- def read_data(file_path):
- for row in open(file_path):
- yield row.split()
- def sum_result(file_path):
- A = X = 1
- B = Y = 2
- C = Z = 3
- scores = {
- ('A', 'X'): 3,
- ('A', 'Y'): 6,
- ('A', 'Z'): 0,
- ('B', 'X'): 0,
- ('B', 'Z'): 6,
- ('B', 'Y'): 3,
- ('C', 'X'): 6,
- ('C', 'Y'): 0,
- ('C', 'Z'): 3
- }
- results = {
- 'X': {t[0]: t[1] for t in scores if scores[t] == 0},
- 'Y': {t[0]: t[1] for t in scores if scores[t] == 3},
- 'Z': {t[0]: t[1] for t in scores if scores[t] == 6},
- }
- sum_tmp_1 = 0
- sum_tmp_2 = 0
- _vars = vars().copy()
- for x, y in read_data(file_path):
- sum_tmp_1 += (
- scores[(x, y)] + _vars[y]
- )
- z = results.get(y).get(x)
- sum_tmp_2 += _vars[z] + scores[(x, z)]
- return sum_tmp_1, sum_tmp_2
- if __name__ == '__main__':
- FILE_PATH = sys.argv[1] if len(sys.argv) > 1 else FILE_PATH
- print(sum_result(FILE_PATH))
Advertisement
Add Comment
Please, Sign In to add comment