DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DoctorLai
DoctorLai

Posted on

Avent of Code - Day 02 - Rock Paper Scissors

Advent of Code occurs at Dec 01 to 25 where each day, you will need to solve a puzzle. It is Festival and the problem statement is mostly related to Christmas.

Day 2 - Rock Paper Scissor Game

https://adventofcode.com/2022/day/2

image.png

image.png

Q1

file1 = open("0.txt", "r")
score = 0

data = {
        "X": 1,
        "A": 1,
        "Y": 2,
        "B": 2,
        "Z": 3,
        "C": 3
}

def win(a, b):
        if a == b:
                return 0
        if a == 1 and b == 2:
                return -1
        if a == 1 and b == 3:
                return 1
        if a == 2 and b == 1:
                return 1
        if a == 2 and b == 3:
                return -1
        if a == 3 and b == 1:
                return -1
        if a == 3 and b == 2:
                return 1

while True:
        line = file1.readline()
        if not line:
                break
        line = line.strip().split()
        i, j = line
        a, b = data[i], data[j]
        if win(b, a) > 0:
                score += b + 6
        elif win(b, a) == 0:
                score += b + 3
        else:
                score += b

print(score)
Enter fullscreen mode Exit fullscreen mode

Q2

file1 = open("0.txt", "r")

score = 0

data = {
        "X": 1,
        "A": 1,
        "Y": 2,
        "B": 2,
        "Z": 3,
        "C": 3
}

while True:
        line = file1.readline()
        if not line:
                break
        line = line.strip().split()
        i, j = line
        if j == "Y":
                score += 3 + data[i]
        elif j == "X":
                if data[i] == 1:
                        score += 3
                elif data[i] == 2:
                        score += 1
                elif data[i] == 3:
                        score += 2
        elif j == "Z":
                if data[i] == 1:
                        score += 2 + 6
                elif data[i] == 2:
                        score += 3 + 6
                elif data[i] == 3:
                        score += 1 + 6

print(score)
Enter fullscreen mode Exit fullscreen mode

Here is an interesting read: Rock Paper Scissor and Naive Bayes


Steem to the Moon!

Top comments (0)

18 Useful Github Repositories Every Developer Should Bookmark

>> Check out this classic DEV post <<