feat: part 2 day 2 complete
This commit is contained in:
75
day2/main.py
75
day2/main.py
@@ -11,6 +11,7 @@ for line in lines:
|
||||
rounds[-1].append(moves[0])
|
||||
rounds[-1].append(moves[1].replace("\n", ""))
|
||||
|
||||
# part 1
|
||||
|
||||
move_score = {
|
||||
"X": 1, # rock
|
||||
@@ -44,17 +45,81 @@ def opponent_did_tie(opponent_move, my_move):
|
||||
return my_move == MY_SCISSORS
|
||||
|
||||
|
||||
total_score = 0
|
||||
total_score_part_1 = 0
|
||||
for _round in rounds:
|
||||
opponent_move = _round[0]
|
||||
my_move = _round[1]
|
||||
|
||||
# always add your move to the total score
|
||||
total_score += move_score[my_move]
|
||||
total_score_part_1 += move_score[my_move]
|
||||
|
||||
if opponent_did_tie(opponent_move, my_move):
|
||||
total_score += 3
|
||||
total_score_part_1 += 3
|
||||
elif not opponent_did_win(opponent_move, my_move):
|
||||
total_score += 6
|
||||
total_score_part_1 += 6
|
||||
|
||||
print(total_score)
|
||||
|
||||
print("Part 1: " + str(total_score_part_1))
|
||||
|
||||
# part 2
|
||||
|
||||
move_score = {
|
||||
"rock": 1, # rock
|
||||
"paper": 2, # paper
|
||||
"scissors": 3, # scissors
|
||||
}
|
||||
|
||||
|
||||
def get_tie_move_score(opponent_move):
|
||||
if opponent_move == OPPONENT_ROCK:
|
||||
return move_score["rock"]
|
||||
elif opponent_move == OPPONENT_PAPER:
|
||||
return move_score["paper"]
|
||||
elif opponent_move == OPPONENT_SCISSORS:
|
||||
return move_score["scissors"]
|
||||
|
||||
|
||||
def get_winning_move_score(opponent_move):
|
||||
if opponent_move == OPPONENT_ROCK:
|
||||
return move_score["paper"]
|
||||
elif opponent_move == OPPONENT_PAPER:
|
||||
return move_score["scissors"]
|
||||
elif opponent_move == OPPONENT_SCISSORS:
|
||||
return move_score["rock"]
|
||||
|
||||
|
||||
def get_losing_move_score(opponent_move):
|
||||
if opponent_move == OPPONENT_ROCK:
|
||||
return move_score["scissors"]
|
||||
elif opponent_move == OPPONENT_PAPER:
|
||||
return move_score["rock"]
|
||||
elif opponent_move == OPPONENT_SCISSORS:
|
||||
return move_score["paper"]
|
||||
|
||||
|
||||
LOSE = "X"
|
||||
TIE = "Y"
|
||||
WIN = "Z"
|
||||
|
||||
outcome_score = {
|
||||
LOSE: 0,
|
||||
TIE: 3,
|
||||
WIN: 6,
|
||||
}
|
||||
|
||||
total_score_part_2 = 0
|
||||
for _round in rounds:
|
||||
opponent_move = _round[0]
|
||||
outcome = _round[1]
|
||||
|
||||
total_score_part_2 += outcome_score[outcome]
|
||||
|
||||
if outcome == TIE:
|
||||
total_score_part_2 += get_tie_move_score(opponent_move)
|
||||
elif outcome == WIN:
|
||||
total_score_part_2 += get_winning_move_score(opponent_move)
|
||||
elif outcome == LOSE:
|
||||
total_score_part_2 += get_losing_move_score(opponent_move)
|
||||
|
||||
|
||||
print("Part 2: " + str(total_score_part_2))
|
||||
|
||||
Reference in New Issue
Block a user