From cf4757ad43823aafa4c6c0547265359e961245a8 Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Fri, 2 Dec 2022 10:18:18 +0100 Subject: Added day2 --- day2/__init__.py | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 day2/__init__.py (limited to 'day2/__init__.py') diff --git a/day2/__init__.py b/day2/__init__.py new file mode 100644 index 0000000..19faab9 --- /dev/null +++ b/day2/__init__.py @@ -0,0 +1,98 @@ +# -*- 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)) + ) -- cgit v1.2.3