Skip to content

Instantly share code, notes, and snippets.

@gnyman
Created December 3, 2022 19:24
Embed
What would you like to do?
# Open the file and read the strategy guide
with open('strategy_guide.txt') as file:
strategy_guide = [line.strip() for line in file]
# Define the score for each shape
SHAPE_SCORES = {'R': 1, 'P': 2, 'S': 3}
# Define the score for each outcome
OUTCOME_SCORES = {'X': 0, 'Y': 3, 'Z': 6}
# Initialize the total score to 0
total_score = 0
# Loop through each pair of characters in the strategy guide
for guide in strategy_guide:
# Split the guide into two values
opponent_choice, our_choice = guide.split()
# Choose the shape that will allow us to achieve the desired outcome
if our_choice == 'X':
# We need to lose the round, so choose the shape that will lose to the opponent's shape
if opponent_choice == 'R':
# The opponent chose Rock, so we need to choose Scissors
our_shape = 'S'
elif opponent_choice == 'S':
# The opponent chose Scissors, so we need to choose Paper
our_shape = 'P'
else:
# The opponent chose Paper, so we need to choose Rock
our_shape = 'R'
elif our_choice == 'Y':
# We need to draw the round, so choose the shape that will draw with the opponent's shape
if opponent_choice == 'R':
# The opponent chose Rock, so we need to choose Rock
our_shape = 'R'
elif opponent_choice == 'S':
# The opponent chose Scissors, so we need to choose Scissors
our_shape = 'S'
else:
# The opponent chose Paper, so we need to choose Paper
our_shape = 'P'
else:
# We need to win the round, so choose the shape that will win against the opponent's shape
if opponent_choice == 'R':
# The opponent chose Rock, so we need to choose Paper
our_shape = 'P'
elif opponent_choice == 'S':
# The opponent chose Scissors, so we need to choose Rock
our_shape = 'R'
else:
# The opponent chose Paper, so we need to choose Scissors
our_shape = 'S'
# Calculate the score for the round
round_score = SHAPE_SCORES[our_shape] + OUTCOME_SCORES[our_choice]
# Add the score for the round to the total score
total_score += round_score
# Print the total score
print(total_score)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment