# -*- coding: utf-8 -*- from abc import ABC from enum import Enum from typing import Iterator from aoc import BaseAssignment class TheirMove(Enum): Rock = "A" Paper = "B" Scissors = "C" class YourMove(Enum): Rock = "X" Paper = "Y" Scissors = "Z" class GameOutcome(Enum): Lose = "X" Draw = "Y" Win = "Z" class Assignment(BaseAssignment, ABC): def calculate_score(self, opponent: TheirMove, you: YourMove) -> int: game_result_score = { TheirMove.Rock: { YourMove.Rock: 3, YourMove.Paper: 6, YourMove.Scissors: 0, }, TheirMove.Paper: { YourMove.Rock: 0, YourMove.Paper: 3, YourMove.Scissors: 6, }, TheirMove.Scissors: { YourMove.Rock: 6, YourMove.Paper: 0, YourMove.Scissors: 3, }, } you_thrown_score = { YourMove.Rock: 1, YourMove.Paper: 2, YourMove.Scissors: 3, } return game_result_score[opponent][you] + you_thrown_score[you] def play_game(self, col1: str, col2: str) -> int: raise NotImplementedError() def run(self, input: Iterator) -> int: return sum([self.play_game(*game.split(" ")) for game in input]) class AssignmentOne(Assignment): example_result = 15 def play_game(self, col1: str, col2: str) -> int: return self.calculate_score(TheirMove(col1), YourMove(col2)) class AssignmentTwo(Assignment): example_result = 12 def calculate_move( self, opponent: TheirMove, game_outcome: GameOutcome ) -> YourMove: next_move = { TheirMove.Rock: { GameOutcome.Lose: YourMove.Scissors, GameOutcome.Draw: YourMove.Rock, GameOutcome.Win: YourMove.Paper, }, TheirMove.Paper: { GameOutcome.Lose: YourMove.Rock, GameOutcome.Draw: YourMove.Paper, GameOutcome.Win: YourMove.Scissors, }, TheirMove.Scissors: { GameOutcome.Lose: YourMove.Paper, GameOutcome.Draw: YourMove.Scissors, GameOutcome.Win: YourMove.Rock, }, } return next_move[opponent][game_outcome] def play_game(self, col1: str, col2: str) -> int: return self.calculate_score( TheirMove(col1), self.calculate_move(TheirMove(col1), GameOutcome(col2)) )