b.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import os, argparse
  2. handEncoding = {"A": "rock", "B": "paper", "C": "scissors"}
  3. resultEncoding = {"X": "lose", "Y": "draw", "Z": "win"}
  4. scoring = {"rock": 1, "paper": 2, "scissors": 3}
  5. beats = {"rock": "paper", "paper": "scissors", "scissors": "rock"}
  6. loses_to = {l: b for b,l in beats.items()}
  7. def throw_hands(me, them):
  8. if me == them:
  9. return 3
  10. if me == beats[them]:
  11. return 6
  12. else:
  13. return 0
  14. def arranged_match(them, result):
  15. if result == "win":
  16. return 6 + scoring[beats[them]]
  17. elif result == "lose":
  18. return scoring[loses_to[them]]
  19. else:
  20. return 3 + scoring[them]
  21. def solve_task(lines):
  22. score = 0
  23. for line in lines:
  24. them = handEncoding[line[0]]
  25. result = resultEncoding[line[-1]]
  26. score += arranged_match(them, result)
  27. print(f"Score: {score}")
  28. def read_lines(filename):
  29. lines = []
  30. with open(filename) as infile:
  31. for raw_line in infile:
  32. line = raw_line.rstrip()
  33. lines.append(line)
  34. return lines
  35. def parse_arguments():
  36. parser = argparse.ArgumentParser(description="Script that solves the case",epilog="Have a nice day!")
  37. parser.add_argument('filename', nargs='?', default="example.txt", help='Input file')
  38. args = parser.parse_args()
  39. return args
  40. def main():
  41. args = parse_arguments()
  42. lines = read_lines(args.filename)
  43. solve_task(lines)
  44. if __name__ == "__main__":
  45. main()