From a419ffdfdf26e4441ee3e3366ae82f13a363edb5 Mon Sep 17 00:00:00 2001 From: Giovani Date: Fri, 2 Dec 2022 23:23:00 -0500 Subject: [PATCH] feat: part 2 day 2 complete --- day2/main.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 70 insertions(+), 5 deletions(-) diff --git a/day2/main.py b/day2/main.py index 3ac99cc..41f91fe 100644 --- a/day2/main.py +++ b/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))