|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +importos,grid,graph,dot,assembly,re,itertools |
| 3 | +fromcollectionsimportCounter,deque,defaultdict |
| 4 | + |
| 5 | +fromcompassimport* |
| 6 | + |
| 7 | + |
| 8 | +# This functions come from https://github.com/mcpower/adventofcode - Thanks! |
| 9 | +deflmap(func,*iterables): |
| 10 | +returnlist(map(func,*iterables)) |
| 11 | + |
| 12 | + |
| 13 | +defints(s:str): |
| 14 | +returnlmap(int,re.findall(r"-?\d+",s))# thanks mserrano! |
| 15 | + |
| 16 | + |
| 17 | +defpositive_ints(s:str): |
| 18 | +returnlmap(int,re.findall(r"\d+",s))# thanks mserrano! |
| 19 | + |
| 20 | + |
| 21 | +deffloats(s:str): |
| 22 | +returnlmap(float,re.findall(r"-?\d+(?:\.\d+)?",s)) |
| 23 | + |
| 24 | + |
| 25 | +defpositive_floats(s:str): |
| 26 | +returnlmap(float,re.findall(r"\d+(?:\.\d+)?",s)) |
| 27 | + |
| 28 | + |
| 29 | +defwords(s:str): |
| 30 | +returnre.findall(r"[a-zA-Z]+",s) |
| 31 | + |
| 32 | + |
| 33 | +test_data= {} |
| 34 | + |
| 35 | +test=1 |
| 36 | +test_data[test]= { |
| 37 | +"input":"""mxmxvkd kfcds sqjhc nhms (contains dairy, fish) |
| 38 | +trh fvjkl sbzzf mxmxvkd (contains dairy) |
| 39 | +sqjhc fvjkl (contains soy) |
| 40 | +sqjhc mxmxvkd sbzzf (contains fish)""", |
| 41 | +"expected": ["5","mxmxvkd,sqjhc,fvjkl"], |
| 42 | +} |
| 43 | + |
| 44 | +test="real" |
| 45 | +input_file=os.path.join( |
| 46 | +os.path.dirname(__file__), |
| 47 | +"Inputs", |
| 48 | +os.path.basename(__file__).replace(".py",".txt"), |
| 49 | +) |
| 50 | +test_data[test]= { |
| 51 | +"input":open(input_file,"r+").read(), |
| 52 | +"expected": ["2410","tmp,pdpgm,cdslv,zrvtg,ttkn,mkpmkx,vxzpfp,flnhl"], |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +# -------------------------------- Control program execution ------------------------- # |
| 57 | + |
| 58 | +case_to_test="real" |
| 59 | +part_to_test=2 |
| 60 | + |
| 61 | +# -------------------------------- Initialize some variables ------------------------- # |
| 62 | + |
| 63 | +puzzle_input=test_data[case_to_test]["input"] |
| 64 | +puzzle_expected_result=test_data[case_to_test]["expected"][part_to_test-1] |
| 65 | +puzzle_actual_result="Unknown" |
| 66 | + |
| 67 | + |
| 68 | +# -------------------------------- Actual code execution ----------------------------- # |
| 69 | + |
| 70 | +all_ingredients=defaultdict(int) |
| 71 | +all_allergens= {} |
| 72 | +nb_allergens=defaultdict(int) |
| 73 | +allergens_ingredients= {} |
| 74 | + |
| 75 | +forstringinpuzzle_input.split("\n"): |
| 76 | +if"contains"instring: |
| 77 | +ingredients=string.split(" (")[0].split(" ") |
| 78 | +allergens=string.split("(contains ")[1][:-1].split(", ") |
| 79 | +ifisinstance(allergens,str): |
| 80 | +allergens= [allergens] |
| 81 | + |
| 82 | +forallergeninallergens: |
| 83 | +nb_allergens[allergen]+=1 |
| 84 | +ifallergennotinall_allergens: |
| 85 | +all_allergens[allergen]=ingredients.copy() |
| 86 | +allergens_ingredients[allergen]=defaultdict(int) |
| 87 | +allergens_ingredients[allergen].update( |
| 88 | + {ingredient:1foringredientiningredients} |
| 89 | + ) |
| 90 | + |
| 91 | +else: |
| 92 | +foringredientiningredients: |
| 93 | +allergens_ingredients[allergen][ingredient]+=1 |
| 94 | +foringredientinall_allergens[allergen].copy(): |
| 95 | +ifingredientnotiningredients: |
| 96 | +all_allergens[allergen].remove(ingredient) |
| 97 | + |
| 98 | +foringredientiningredients: |
| 99 | +all_ingredients[ingredient]+=1 |
| 100 | + |
| 101 | +else: |
| 102 | +print("does not contain any allergen") |
| 103 | + |
| 104 | + |
| 105 | +forallergenintest: |
| 106 | +ifallergen!="shellfish": |
| 107 | +continue |
| 108 | +print( |
| 109 | +allergen, |
| 110 | +test2[allergen], |
| 111 | + [ingforing,valintest[allergen].items()ifval==test2[allergen]], |
| 112 | + ) |
| 113 | + |
| 114 | +sum_ingredients=0 |
| 115 | +foringredientinall_ingredients: |
| 116 | +ifnot (any(ingredientinvalforvalinall_allergens.values())): |
| 117 | +sum_ingredients+=all_ingredients[ingredient] |
| 118 | + |
| 119 | +ifpart_to_test==1: |
| 120 | +puzzle_actual_result=sum_ingredients |
| 121 | + |
| 122 | + |
| 123 | +else: |
| 124 | +allergens_ingredients= { |
| 125 | +aller: [ |
| 126 | +ing |
| 127 | +foring,valinallergens_ingredients[aller].items() |
| 128 | +ifval==nb_allergens[aller] |
| 129 | + ] |
| 130 | +forallerinnb_allergens |
| 131 | + } |
| 132 | +final_allergen= {} |
| 133 | +whilelen(final_allergen)!=len(nb_allergens): |
| 134 | +forallergen,valinallergens_ingredients.items(): |
| 135 | +iflen(val)==1: |
| 136 | +final_allergen[allergen]=val[0] |
| 137 | + |
| 138 | +allergens_ingredients= { |
| 139 | +aller: [ |
| 140 | +ing |
| 141 | +foringinallergens_ingredients[aller] |
| 142 | +ifingnotinfinal_allergen.values() |
| 143 | + ] |
| 144 | +forallerinnb_allergens |
| 145 | + } |
| 146 | + |
| 147 | +print(final_allergen) |
| 148 | +ing_list="" |
| 149 | +forallerinsorted(final_allergen.keys()): |
| 150 | +ing_list+=final_allergen[aller]+"," |
| 151 | +puzzle_actual_result=ing_list[:-1] |
| 152 | + |
| 153 | +# -------------------------------- Outputs / results --------------------------------- # |
| 154 | + |
| 155 | +print("Case :",case_to_test,"- Part",part_to_test) |
| 156 | +print("Expected result : "+str(puzzle_expected_result)) |
| 157 | +print("Actual result : "+str(puzzle_actual_result)) |
| 158 | +# Date created: 2020-12-21 06:07:34.505688 |
| 159 | +# Part 1: 2020-12-21 07:22:36 |
| 160 | +# Part 2: 2020-12-21 07:30:15 |