Skip to content

Instantly share code, notes, and snippets.

@dartharva
Created December 2, 2022 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dartharva/a8d90725a8563c6249139c4c56b20c28 to your computer and use it in GitHub Desktop.
Save dartharva/a8d90725a8563c6249139c4c56b20c28 to your computer and use it in GitHub Desktop.
Advent of Code 2022, Day 2 - Simple, verbose python.
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