From ed2497a67c4b941a2642193b42991637c660eed1 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 2 Dec 2022 08:05:39 +0100 Subject: [PATCH] 2022: d02: ex1: add solution --- 2022/d02/ex1/ex1.py | 61 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 2022/d02/ex1/ex1.py diff --git a/2022/d02/ex1/ex1.py b/2022/d02/ex1/ex1.py new file mode 100755 index 0000000..30900c8 --- /dev/null +++ b/2022/d02/ex1/ex1.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python + +import enum +import sys + + +class Outcome(enum.IntEnum): + Lose = 0 + Draw = 3 + Win = 6 + + +class Choice(enum.IntEnum): + Rock = 1 + Paper = 2 + Scissors = 3 + + @classmethod + def from_input(cls, input: str) -> "Choice": + match input: + case "A" | "X": + return cls.Rock + case "B" | "Y": + return cls.Paper + case "C" | "Z": + return cls.Scissors + assert False # Sanity check + + +def score(other: Choice, you: Choice) -> int: + def outcome() -> Outcome: + if other == you: + return Outcome.Draw + if other == other.Rock and you == other.Paper: + return Outcome.Win + if other == other.Paper and you == other.Scissors: + return Outcome.Win + if other == other.Scissors and you == other.Rock: + return Outcome.Win + return Outcome.Lose + + return you + outcome() + + +Round = tuple[Choice, Choice] + + +def solve(input: list[Round]) -> int: + return sum(map(lambda round: score(*round), input)) + + +def main() -> None: + input: list[Round] = [ + (Choice.from_input(round[0]), Choice.from_input(round[2])) + for round in sys.stdin.readlines() + ] + print(solve(input)) + + +if __name__ == "__main__": + main()