Avent of Code - Day 02 - Rock Paper ScissorssteemCreated with Sketch.

in #adventofcodelast year

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)

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)

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


Steem to the Moon!

Coin Marketplace

STEEM 0.36
TRX 0.12
JST 0.039
BTC 69965.85
ETH 3540.49
USDT 1.00
SBD 4.71