From 0a441ce2306ae9edefb009fbc0596f4d511cdc7f Mon Sep 17 00:00:00 2001 From: blenovo Date: Wed, 6 Aug 2025 00:51:06 +0200 Subject: summertime warmup session - finish --- 2020/aoc2020-d21.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 2020/aoc2020-d21.py (limited to '2020/aoc2020-d21.py') diff --git a/2020/aoc2020-d21.py b/2020/aoc2020-d21.py new file mode 100644 index 0000000..f6edea8 --- /dev/null +++ b/2020/aoc2020-d21.py @@ -0,0 +1,41 @@ +#advent of code 2020 +#day 21 +#part 2 very similar to ticket problem from the same year + +ingredients = []; +AllergyDict = dict(); +PossibleAllergens = set(); +AssignedAllergens = dict(); + +PuzzleInput = open("21.in","r"); +for line in PuzzleInput: + line = line.replace("\n",""); + FoodIngr, FoodAllr = line[:-1].split(" (contains "); + FoodIngr = FoodIngr.split(" "); + FoodAllr = FoodAllr.split(", "); + ingredients.extend(FoodIngr); + for a in FoodAllr: + if a in AllergyDict: + AllergyDict[a].intersection_update(set(FoodIngr)); + else: + AllergyDict[a] = set(FoodIngr); +PuzzleInput.close(); + +for al in AllergyDict: + print(al, AllergyDict[al]); + PossibleAllergens.update(AllergyDict[al]); + +p1 = len([i for i in ingredients if i not in PossibleAllergens]); +print("part 1 =",p1); + +while len(AssignedAllergens) != len(AllergyDict): + #print(len(AssignedAllergens),"/", len(AllergyDict)); #in case it takes long + for al in AllergyDict: + if len(AllergyDict[al]) == 1: + found = AllergyDict[al].pop(); + AssignedAllergens[al] = found; + else: + AllergyDict[al].difference_update( AssignedAllergens.values() ); + +p2 = ",".join([AssignedAllergens[allergen] for allergen in sorted(AssignedAllergens.keys())]); +print("part 2 =",p2); -- cgit v1.2.3