From e29575cb35d9daaccc82c306a2ad74dd87636335 Mon Sep 17 00:00:00 2001 From: Peery Date: Fri, 2 Dec 2022 15:14:38 +0100 Subject: [PATCH] Day 2 (Puzzle 2) - Rock, Paper, Scissor Successfully finished the second puzzle. The parsing changed so that the second column is not my answer but the intended result. I just quickly changed the file parser to replace the intended result with the fitting answer so that I need not touch the evaluation function. Moved the previous parsing to parse_file_v1() for archiving purposes. --- rockpaperscissor/file/guide_list.py | 45 ++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/rockpaperscissor/file/guide_list.py b/rockpaperscissor/file/guide_list.py index d34d2bd..1a87cd7 100644 --- a/rockpaperscissor/file/guide_list.py +++ b/rockpaperscissor/file/guide_list.py @@ -3,6 +3,10 @@ from typing import List, Tuple class RPS: + WIN_SYM = "Z" + DRAW_SYM = "Y" + LOSS_SYM = "X" + ROCK = "R" PAPER = "P" SCISSOR = "S" @@ -47,6 +51,10 @@ class RockPaperScissorGuide: The score for a single round is the score for the shape you selected (1 for Rock, 2 for Paper, and 3 for Scissors) plus the score for the outcome of the round (0 if you lost, 3 if the round was a draw, and 6 if you won). + + (Part 2 only) + The second column says how the round needs to end: X means you need to lose, + Y means you need to end the round in a draw, and Z means you need to win. """ def __init__(self, path: str): @@ -55,7 +63,7 @@ class RockPaperScissorGuide: self.file_path = path - def parse_file(self) -> List[Tuple[str, str]]: + def parse_file_v1(self) -> List[Tuple[str, str]]: strategy_guide = [] with open(self.file_path, "r") as file: for line in file: @@ -64,3 +72,38 @@ class RockPaperScissorGuide: strategy_guide.append((RPS.RPS_SYM_TO_SHAPE[a], RPS.RPS_SYM_TO_SHAPE[b])) return strategy_guide + + def parse_file(self) -> List[Tuple[str, str]]: + strategy_guide = [] + with open(self.file_path, "r") as file: + for line in file: + line = line.replace("\n", "") + a, b = line.split(" ") + + a = RPS.RPS_SYM_TO_SHAPE[a] + + answer = None + + if b == RPS.WIN_SYM: + if a == RPS.ROCK: + answer = RPS.PAPER + elif a == RPS.PAPER: + answer = RPS.SCISSOR + elif a == RPS.SCISSOR: + answer = RPS.ROCK + elif b == RPS.DRAW_SYM: + answer = a + elif b == RPS.LOSS_SYM: + if a == RPS.ROCK: + answer = RPS.SCISSOR + elif a == RPS.PAPER: + answer = RPS.ROCK + elif a == RPS.SCISSOR: + answer = RPS.PAPER + + if answer is None: + raise ValueError("Something went wrong with answer determination!") + + strategy_guide.append((a, answer)) + + return strategy_guide