Skip to content

Instantly share code, notes, and snippets.

@josfam
Last active June 8, 2023 13:39
Show Gist options
  • Save josfam/a834597a30002a9146d3351f846201c5 to your computer and use it in GitHub Desktop.
Save josfam/a834597a30002a9146d3351f846201c5 to your computer and use it in GitHub Desktop.
Accompanying code for Advent of Code 2022 Day 2: Rock Paper Scissors Medium article
sample_input = """
A Y
B X
C Z
""".strip().splitlines()
# rock, paper, scissors information from our perspective
stats = {
'X': {'score': 1, 'beats': 'C', 'beaten by': 'B', 'draws with': 'A'},
'Y': {'score': 2, 'beats': 'A', 'beaten by': 'C', 'draws with': 'B'},
'Z': {'score': 3, 'beats': 'B', 'beaten by': 'A', 'draws with': 'C'},
}
WIN = 6
DRAW = 3
LOSS = 0
our_total_score = 0
for game_round in sample_input:
# extract the opponent's move, and ours
opponent_move, our_move = game_round.strip().split()
# note our move's score
our_move_score = stats[our_move]['score']
outcome_score = 0
# update the outcome based on if we played a
# winning, drawing, or losing move
if stats[our_move]['beats'] == opponent_move:
outcome_score += WIN
elif stats[our_move]['beaten by'] == opponent_move:
outcome_score += LOSS
else:
outcome_score += DRAW
our_total_score += (our_move_score + outcome_score)
print(our_total_score)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment