Day 2

Part 1

  • Read in input file contain list of moves
  • each line contains 2 pairs:
    • the first column is the opponents move: Rock (A) Paper(B) Scissors(C)
    • the second column is your move Rock (X) Paper(Y) Scissors (Z)
with open("day2/input.txt") as f:
    input = f.read().splitlines()

pprint(input[:5])
['B Y', 'A Z', 'C Z', 'A Y', 'A Y']

match_value

 match_value (your_piece:str, opp_piece:str)

Returns the value of the match between your piece and the opponent’s piece - if equal, returns 3, else if you win then return 6 else return 0

Type Details
your_piece str your piece (Rock/Paper/Scissors)
opp_piece str opponent’s piece (Rock/Paper/Scissors)
Returns int
print(match_value("Rock","Paper"))
print(match_value("Rock","Scissors"))
print(match_value("Paper","Paper"))
print(match_value("Paper","Scissors"))
0
6
3
0
  • Create a function that computes the score for each line

score

 score (input:str)

Returns the score of each move by you and your opponent based on the combination of the match value plus the piece value.

Your opponent’s and your move (Rock,Paper,Scissors) which are encoded A,B,C and by X,Y,Z respectively.

Type Details
input str a string containing your opponent’s and your move separated by a space
Returns int
  • For each sequence of numbers punctuated by an empty line, sum up calories for each
  • If sum of calories is greater than current max, set that as the current max
print(score("A Y"))
print(score("B X"))
print(score("C Z"))
8
1
6
  • Finally the total score

Click on the Answer tab to view the answer

total_score = sum([score(l) for l in input])
print(f'the correct answer for part 1 is {total_score}')
the correct answer for part 1 is 10404

Part 2

  • Reinterpret your move to follow a strategy:
    • X means LOSE, Y means DRAW, Z means WIN
  • Create a function that finds the piece to fulfill a strategy (WIN,LOSE or DRAW) based on the opponent piece

find_strat_piece

 find_strat_piece (opp_piece:str, your_strat:str)

Finds the piece that matches the strategy you picked given the opponent’s piece

Type Details
opp_piece str your opponents piece (Rock, Paper,Scissors)
your_strat str your strategy (WIN,LOSE, DRAW)
Returns str
print(find_strat_piece("Rock","DRAW"))
print(find_strat_piece("Scissors","WIN"))
print(find_strat_piece("Paper","LOSE"))
Rock
Rock
Rock
  • Create a function that computes the score for each line based on the second item being the strategy

score_strat_action

 score_strat_action (input:str)

Returns the score given the opponents move and your strategy

Type Details
input str a string containing your opponent’s move and your strategy separated by a space
Returns int
print(score_strat_action("A Y"))
print(score_strat_action("B X"))
print(score_strat_action("C Z"))
4
1
7

Click on the Answer tab to view the answer

total_strat_score = sum([score_strat_action(l) for l in input])
print(f'the correct answer for part 2 is {total_strat_score}')
the correct answer for part 2 is 10334