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.
main
Peery 2 years ago
parent a77ad30e10
commit e29575cb35

@ -3,6 +3,10 @@ from typing import List, Tuple
class RPS: class RPS:
WIN_SYM = "Z"
DRAW_SYM = "Y"
LOSS_SYM = "X"
ROCK = "R" ROCK = "R"
PAPER = "P" PAPER = "P"
SCISSOR = "S" 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) 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). 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): def __init__(self, path: str):
@ -55,7 +63,7 @@ class RockPaperScissorGuide:
self.file_path = path self.file_path = path
def parse_file(self) -> List[Tuple[str, str]]: def parse_file_v1(self) -> List[Tuple[str, str]]:
strategy_guide = [] strategy_guide = []
with open(self.file_path, "r") as file: with open(self.file_path, "r") as file:
for line in 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])) strategy_guide.append((RPS.RPS_SYM_TO_SHAPE[a], RPS.RPS_SYM_TO_SHAPE[b]))
return strategy_guide 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

Loading…
Cancel
Save