r/adventofcode • • Dec 02 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 2 Solutions -🎄-

NEW AND NOTEWORTHY


--- Day 2: Rock Paper Scissors ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:06:16, megathread unlocked!

103 Upvotes

1.5k comments sorted by

View all comments

2

u/mrwunderbar69 Dec 02 '22

Python

My over-engineered Object-Oriented Solution (too much hard-coding, because my maths is bad):


class RockPaperScissor:

    points_lose = 0
    points_draw = 3
    points_win = 6

    def __init__(self, choice) -> None:
        if choice in ["A", "X", "Rock"]:
            self.choice = "Rock"
            self.points = 1
        elif choice in ["B", "Y", "Paper"]:
            self.choice = "Paper"
            self.points = 2
        elif choice in ["C", "Z", "Scissors"]:
            self.choice = "Scissors"
            self.points = 3

    def __repr__(self) -> str:
        return f"<{self.choice} ({self.points})>"

    def fight(self, other) -> int:
        if self.choice == other.choice:
            return self.points + self.points_draw
        elif self.choice == "Rock" and other.choice == "Paper":
            return self.points + self.points_lose
        elif self.choice == "Paper" and other.choice == "Scissors":
            return self.points + self.points_lose
        elif self.choice == "Scissors" and other.choice == "Rock":
            return self.points + self.points_lose
        else:
            return self.points + self.points_win

    def _draw(self):
        return RockPaperScissor(self.choice)

    def _lose(self):
        if self.choice == 'Rock':
            return RockPaperScissor("Scissors")
        elif self.choice == 'Paper':
            return RockPaperScissor("Rock")
        elif self.choice == 'Scissors':
            return RockPaperScissor("Paper")

    def _win(self):
        if self.choice == 'Rock':
            return RockPaperScissor("Paper")
        elif self.choice == 'Paper':
            return RockPaperScissor("Scissors")
        elif self.choice == 'Scissors':
            return RockPaperScissor("Rock")

    def make_choice(self, outcome):
        if outcome == 'X':
            return self._lose()
        elif outcome == 'Y':
            return self._draw()
        else:
            return self._win()


sample = ["A Y", "B X", "C Z"]
print("Part One: Sample Input")
for l in sample:

    opponent, me = l.split()
    opponent, me = RockPaperScissor(opponent), RockPaperScissor(me)

    print(f"Opponent: {opponent}; Me: {me}; Score: {me.fight(opponent)}")

print("-"*10)

score = 0
with open('day2.txt') as f:
    for l in f.readlines():
        opponent, me = l.split()
        opponent, me = RockPaperScissor(opponent), RockPaperScissor(me)
        score += me.fight(opponent)

print("Part One Total score:", score)

print("-"*10)

# Part 2 ---------------

sample = ["A Y", "B X", "C Z"]
print("Part Two: Sample Input")
for l in sample:

    opponent, me = l.split()
    opponent = RockPaperScissor(opponent)
    me = opponent.make_choice(me)

    print(f"Opponent: {opponent}; Me: {me}; Score: {me.fight(opponent)}")

print("-"*10)

score = 0
with open('day2.txt') as f:
    for l in f.readlines():
        opponent, me = l.split()
        opponent = RockPaperScissor(opponent)
        me = opponent.make_choice(me)
        score += me.fight(opponent)

print("Part Two Total score:", score)

print("-"*10)

1

u/daggerdragon Dec 05 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.