Advent of code

 15 décembre 2015 

  Science for Hungry People :
  1. code.py
  2. codeRepartitionNonRecursif.py
  3. codeRepartitionOneLiner.py
  4. maths.py
TOT_QUANTITES = 100
NB_CALORIES_VOULUES = 500
STATS_INGREDIENTS = {
    'Sprinkles':    {'capacity':2, 'durability':0,  'flavor':-2, 'texture':0 , 'calories':3},
    'Butterscotch': {'capacity':0, 'durability':5,  'flavor':-3, 'texture':0 , 'calories':3},
    'Chocolate':    {'capacity':0, 'durability':0,  'flavor':5 , 'texture':-1, 'calories':8},
    'Candy':        {'capacity':0, 'durability':-1, 'flavor':0 , 'texture':5 , 'calories':8}
}
INGREDIENTS = list(STATS_INGREDIENTS.keys())

def score(quantites):
    score = 1
    for propriete in ['capacity', 'durability', 'flavor', 'texture']:
        somme = 0
        for ingredient in quantites:
            somme += quantites[ingredient] * STATS_INGREDIENTS[ingredient][propriete]
        score *= max(0, somme)
    return score

def calories(quantites):
    return sum([quantites[ingredient] * STATS_INGREDIENTS[ingredient]['calories'] for ingredient in quantites])   



def repartitions(nb_billes, nb_godets):
    # if nb_billes < 0 or nb_godets < 0:
    #     raise ValueError('values must be non negative')
    # if nb_godets == 0:
    #     if nb_billes == 0:
    #         return [[]]
    #     else:
    #         return []
    # if nb_billes == 0:
    #     return [nb_godets * [0]]
    if nb_godets == 1:
        return [[nb_billes]]
    liste = []
    for nb in range(nb_billes + 1):
        liste.extend([[nb] + t for t in repartitions(nb_billes - nb, nb_godets - 1)])
    return liste




maxScore = 0
maxScorePartie2 = 0

for repartition in repartitions(TOT_QUANTITES, len(STATS_INGREDIENTS)):
    quantites = dict(zip(INGREDIENTS, repartition))
    theScore = score(quantites)

    maxScore = max(maxScore, theScore)

    if (calories(quantites) == NB_CALORIES_VOULUES):
        maxScorePartie2 = max(maxScorePartie2, theScore)

print(maxScore, maxScorePartie2)