#!/usr/bin/env python3
#Advent of Code
#2015 Day 15, Part 1
#Solution by James C. (https://github.com/JamesMCo)
importos,syssys.path.append(os.path.abspath("../.."))importunittest,util.readfromutil.testsimportrundefsolve(puzzle_input):ingredients=[]foringredientinpuzzle_input:ingredient=ingredient.split()ingredients.append([int(x)forxin[ingredient[2][:-1],ingredient[4][:-1],ingredient[6][:-1],ingredient[8][:-1],ingredient[10]]])defscore(amounts):running=[]foriinrange(4):working=0forj,amountinenumerate(amounts):working+=amount*ingredients[j][i]ifworking<0:running.append(0)else:running.append(working)working=1forvalinrunning:working*=valreturnworkingdefincrement(quant_list):i=len(quant_list)-1whilei>=0:quant_list[i]+=1ifquant_list[i]==101:quant_list[i]=0i-=1else:breakreturnquant_listhighest=0quants=[0forxinrange(len(ingredients))]end_state=[100forxinrange(len(ingredients))]whileTrue:must_run=Truewhilesum(quants)!=100ormust_run:must_run=Falsequants=increment(quants)ifquants==end_state:returnhighesttemp=score(quants)iftemp>highest:highest=tempdefmain():puzzle_input=util.read.as_lines()highest=solve(puzzle_input)print("The total score of the highest-scoring cookie is "+str(highest)+".")classAOC_Tests(unittest.TestCase):deftest_ex1(self):returnself.assertEqual(solve(["Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8","Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3"]),62842880)if__name__=="__main__":run(main)
#!/usr/bin/env python3
#Advent of Code
#2015 Day 15, Part 2
#Solution by James C. (https://github.com/JamesMCo)
importos,syssys.path.append(os.path.abspath("../.."))importunittest,util.readfromutil.testsimportrundefsolve(puzzle_input):ingredients=[]foringredientinpuzzle_input:ingredient=ingredient.split()ingredients.append([int(x)forxin[ingredient[2][:-1],ingredient[4][:-1],ingredient[6][:-1],ingredient[8][:-1],ingredient[10]]])defscore(amounts):running=[]foriinrange(4):working=0forj,amountinenumerate(amounts):working+=amount*ingredients[j][i]ifworking<0:running.append(0)else:running.append(working)working=1forvalinrunning:working*=valreturnworkingdefvalid(amounts):running=0fori,amountinenumerate(amounts):running+=amount*ingredients[i][4]returnrunning==500defincrement(quant_list):i=len(quant_list)-1whilei>=0:quant_list[i]+=1ifquant_list[i]==101:quant_list[i]=0i-=1else:breakreturnquant_listhighest=0quants=[0forxinrange(len(ingredients))]end_state=[100forxinrange(len(ingredients))]whileTrue:must_run=Truewhilesum(quants)!=100ormust_run:must_run=Falsequants=increment(quants)ifquants==end_state:returnhighestifvalid(quants):temp=score(quants)iftemp>highest:highest=tempdefmain():puzzle_input=util.read.as_lines()highest=solve(puzzle_input)print("The total score of the highest-scoring cookie is "+str(highest)+".")classAOC_Tests(unittest.TestCase):deftest_ex1(self):returnself.assertEqual(solve(["Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8","Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3"]),57600000)if__name__=="__main__":run(main)