#!/usr/bin/env python3 # opponent: a rock, b paper, c scissors # ME : x rock , y paper, c scissors # Score: 1 R 2 P 3 S # + 0 lost 3 draw 6 won # same: draw # Rock over scissors # Scissors over paper # paper over rock scores = {"X": 1, "Y": 2, "Z": 3} plays = { "A": "X", "B": "Y", "C": "Z", "Z": "C", "Y": "B", "X": "A", } # rock # paper # scissors must = {"X": "lose", "Y": "draw", "Z": "win"} def choose(opponent: str, ending: str) -> str: """ >>> choose("A", "X") # rock, must lose "C" """ if ending == "Y": # draw return plays[opponent] if opponent == "A": if ending == "X": # lose return "Z" else: # win return "Y" if opponent == "B": if ending == "X": return "X" else: return "Z" if opponent == "C": if ending == "X": return "Y" else: return "X" def get_score(play: str) -> int: opponent = play[0] us = play[2] if plays[opponent] == us: # draw return 3 + scores[us] if us == "X": # rock if opponent == "C": return 6 + scores[us] else: # loss return scores[us] if us == "Y": # paper if opponent == "A": # rock return 6 + scores[us] else: return scores[us] if us == "Z": # scissors if opponent == "B": return 6 + scores[us] else: return scores[us] def part1(strategy) -> int: score: int = 0 for line in strategy: score += get_score(line.strip()) return score def part2(strategy) -> int: score: int = 0 for line in strategy: line = line.strip() score += get_score("{0} {1}".format(line[0], choose(line[0], line[2]))) return score