Skip to content

Instantly share code, notes, and snippets.

@Shogun89
Created December 11, 2022 20:32
Show Gist options
  • Save Shogun89/c3dd22d82cfd01840649cdd12b2ac749 to your computer and use it in GitHub Desktop.
Save Shogun89/c3dd22d82cfd01840649cdd12b2ac749 to your computer and use it in GitHub Desktop.
Advent Of Code - Day 2 Part 2
def fetch_tournament(file) -> list[str]:
with open(file) as f:
lines = f.readlines()
return lines
def clean_game(game) -> str:
dic = {
'A' : 'Rock',
'B' : 'Paper',
'C' : 'Scissors',
'X' : 'Lose',
'Y' : 'Draw',
'Z' : 'Win'
}
for i,j in dic.items():
game = game.replace(i,j)
return game
def clean_tournament(tournament) -> list[str]:
cleaned_tournament = []
for game in tournament:
cleaned_tournament.append(clean_game((game)))
return cleaned_tournament
def translate_response(opponent_action, response) -> str:
## Win
if response == 'Win' and opponent_action == 'Rock':
return 'Paper'
if response == 'Win' and opponent_action == 'Paper':
return 'Scissors'
if response == 'Win' and opponent_action == 'Scissors':
return 'Rock'
if response == 'Lose' and opponent_action == 'Rock':
return 'Scissors'
if response == 'Lose' and opponent_action == 'Paper':
return 'Rock'
if response == 'Lose' and opponent_action == 'Scissors':
return 'Paper'
## Draw
if response == 'Draw':
return opponent_action
def fetch_score(opponent_action, response) -> int:
shape_score = 0
round_score = 0
## Wins for response
if (
(opponent_action == 'Rock' and response == 'Paper') or \
(opponent_action == 'Paper' and response == 'Scissors') or \
(opponent_action == 'Scissors' and response == 'Rock')
):
round_score = 6
## Draws
if opponent_action == response:
# All draws
round_score = 3
## Losses for response
if (
(opponent_action == 'Paper' and response == 'Rock') or \
(opponent_action == 'Scissors' and response == 'Paper') or \
(opponent_action == 'Rock' and response == 'Scissors')
):
round_score = 0
## Shape score
if response == 'Rock':
shape_score = 1
if response == 'Paper':
shape_score = 2
if response == 'Scissors':
shape_score = 3
return shape_score + round_score
def fetch_tournament_scores(tournament) -> list[int]:
scores = []
for game in tournament:
actions = game.split(' ')
opponent_action = actions[0]
response = actions[1].replace('\n','')
new_response = translate_response(opponent_action, response)
score = fetch_score(opponent_action, new_response)
scores.append(score)
return scores
input_file = 'd2_input.txt'
tournament = fetch_tournament(input_file)
cleaned_tournament = clean_tournament(tournament)
scores = fetch_tournament_scores(cleaned_tournament)
print("Your total score was {}".format(sum(scores)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment