Advent of code

 2 décembre 2022 

  Rock Paper Scissors : Calcul de points selon stratégies données
A Y
   
A Z
   
A X
   
B X
   
A Y
   
 
  1. code.py
  2. code_court.py
CHOIX = {'A':1, 'B':2, 'C':3, 'X':1, 'Y':2, 'Z':3}

DEFAITE  = 'X'
NUL      = 'Y'
VICTOIRE = 'Z'

GAINS = {DEFAITE:0, NUL:3, VICTOIRE:6}

def moduloDe1A3(x):
    return (x-1)%3 + 1    #  ((x-1)%3 + 3)%3 + 1  en JavaScript, car -1%3=-1 en JS

def pointsChoix(choix):   # la fonction la plus utile au monde
    return choix

def coupGagnant(choixElfe):
    return moduloDe1A3(choixElfe + 1)
def coupPerdant(choixElfe):
    return moduloDe1A3(choixElfe - 1)


score1 = 0
score2 = 0

f = open("input.txt", 'r')
for line in f.readlines():
    stratElfe = line[0]
    stratMoi  = line[2]

    choixElfe = CHOIX[stratElfe]

    # pour partie 1
    choixMoi = CHOIX[stratMoi]
    score1 += pointsChoix(choixMoi)

    if choixMoi == choixElfe:
        score1 += GAINS[NUL]
    elif choixMoi == coupGagnant(choixElfe):   # ou  (choixMoi-choixElfe) in [1,-2] ,  ou %3 == 1
        score1 += GAINS[VICTOIRE]
    else:
        score1 += GAINS[DEFAITE]     # inutile puisque vaut 0…

    # pour partie 2
    score2 += GAINS[stratMoi]

    if   VICTOIRE == stratMoi:
            score2 += pointsChoix(coupGagnant(choixElfe))
    elif NUL == stratMoi:
            score2 += pointsChoix(choixElfe)
    elif DEFAITE == stratMoi:
            score2 += pointsChoix(coupPerdant(choixElfe))
    else:
        raise Exception("«%s» n'est pas une stratégie prévue…" % stratMoi)



print("Réponse partie 1:", score1)
print("Réponse partie 2:", score2)