Created
December 2, 2022 07:53
-
-
Save dartharva/a8d90725a8563c6249139c4c56b20c28 to your computer and use it in GitHub Desktop.
Advent of Code 2022, Day 2 - Simple, verbose python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with open('input.txt') as f: | |
rounds = [i.split(' ') for i in f.read().split('\n')] | |
#Part 1 | |
PLAYS = { | |
"X": "rock", | |
"Y": "paper", | |
"Z": "scissors", | |
"A": "rock", | |
"B": "paper", | |
"C": "scissors", | |
} | |
PLAY_SCORE = { | |
"rock": 1, | |
"paper": 2, | |
"scissors": 3, | |
"tie": 3, | |
"win": 6, | |
"loss": 0, | |
} | |
def play_game(rounds): | |
score = [] | |
for i in rounds[:-1]: # IDK why but there's an empty block at the end of the list for some reason | |
elfs_play = PLAYS.get(i[0]) | |
your_play = PLAYS.get(i[-1]) | |
if your_play == elfs_play: | |
outcome = "tie" | |
elif your_play == "rock": | |
if elfs_play == "scissors": | |
outcome = "win" | |
else: | |
outcome = "loss" | |
elif your_play == "paper": | |
if elfs_play == "rock": | |
outcome = "win" | |
else: | |
outcome = "loss" | |
elif your_play == "scissors": | |
if elfs_play == "paper": | |
outcome = "win" | |
else: | |
outcome = "loss" | |
score.append(PLAY_SCORE.get(your_play) + PLAY_SCORE.get(outcome)) | |
return score | |
print(sum(play_game(rounds))) | |
# Part 2 | |
OUTCOME_INDEX = { | |
"X": "loss", | |
"Y": "tie", | |
"Z": "win" | |
} | |
def play_other_game(rounds): | |
other_score = [] | |
for i in rounds[:-1]: | |
elfs_play = PLAYS.get(i[0]) | |
outcome = OUTCOME_INDEX.get(i[-1]) | |
if outcome == "tie": #draw | |
your_play = elfs_play | |
elif outcome == "loss": #lose | |
if elfs_play == "rock": | |
your_play = "scissors" | |
elif elfs_play == "paper": | |
your_play = "rock" | |
elif elfs_play == "scissors": | |
your_play = "paper" | |
elif outcome == "win": #win | |
if elfs_play == "rock": | |
your_play = "paper" | |
elif elfs_play == "paper": | |
your_play = "scissors" | |
elif elfs_play == "scissors": | |
your_play = "rock" | |
other_score.append(PLAY_SCORE.get(your_play) + PLAY_SCORE.get(outcome)) | |
return(other_score) | |
print(sum(play_other_game(rounds))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment