diff options
| author | blenovo <bk@gmail.com> | 2025-08-06 00:51:06 +0200 |
|---|---|---|
| committer | blenovo <bk@gmail.com> | 2025-08-06 00:51:06 +0200 |
| commit | 0a441ce2306ae9edefb009fbc0596f4d511cdc7f (patch) | |
| tree | 11d5aca4383bbce8a35acb912698f473b8180215 /2020/aoc2020-d21.py | |
| parent | 12f551dbfef138880b29ba6abe4576714ae942cb (diff) | |
summertime warmup session - finish
Diffstat (limited to '2020/aoc2020-d21.py')
| -rw-r--r-- | 2020/aoc2020-d21.py | 41 |
1 files changed, 41 insertions, 0 deletions
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); |
