From 6b61c4882b45c3da11659d5c1fd6d6966a83cb0d Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 21 Dec 2020 23:18:07 +0100 Subject: [PATCH] Thing --- 2020/21.py | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100755 2020/21.py diff --git a/2020/21.py b/2020/21.py new file mode 100755 index 0000000..4d50773 --- /dev/null +++ b/2020/21.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python + +from aoc import get_input # AoC +import re # regex +from collections import defaultdict as dd +from copy import deepcopy as copy + +data = get_input(21).splitlines() + +foodlist = [] +allfood = [] + +allerlist = [] + +allerthing = dd(set) + +for food in data: + cont, aller = food.split("(") + cont = cont.strip().split(" ") + aller = aller.replace(")", "").replace("contains ", "").split(", ") + + foodlist.append(cont) + allerlist.append(set(aller)) + + for c in cont: + allfood.append(c) + + +for i, food in enumerate(foodlist): + aller = allerlist[i] + + for al in aller: + if( not al in allerthing.keys() ): + allerthing[al] = set(food) + else: + allerthing[al] &= set(food) + +#print(allerthing) + +for aller, cont in allerthing.items(): + for thing in cont: + while(thing in allfood): + allfood.remove(thing) + +#print(len(allfood)) + +ignorefoods = set( [food for food in allfood] ) + +seenaller = set() +seenfood = set() + +thing = dict() + +for i, food in enumerate(foodlist): + aller = allerlist[i] + # why is there no NAND? :( + + for j, food2 in enumerate(foodlist): + if(i == j): + continue + + aller2 = allerlist[j] + commonaller = set(aller) & set(aller2) + + commonfood = set(food) & set(food2) + + for ignore in ignorefoods: + commonfood.discard(ignore) + + for seen in seenfood: + commonfood.discard(seen) + + for al in commonaller: + if(al in seenaller): + continue + + cfList = list(commonfood) + caList = list(commonaller) + #breakpoint() + if( len(commonfood) == 1 and len(commonaller) == 1 ): + + + thing[cfList[0]] = caList[0] + seenfood.add(cfList[0]) + seenaller.add(caList[0]) + +print(thing)