This commit is contained in:
a 2021-05-13 08:05:22 -05:00
commit 4960ab9dfe
3 changed files with 67 additions and 1 deletions

View File

@ -1,16 +1,58 @@
def main():
with open("input_sample.txt") as f:
with open("input.txt") as f:
lines = f.read().split('\n')
foods = [s[:-1].split(' (contains ') for s in lines]
# map allergens to possible ingredients
possible = [
(allergen, set(food[0].split(' ')))
for food in foods
for allergen in food[1].split(', ')
]
# get sets of all allergens and all ingredients
allergens = {p[0] for p in possible}
ingredients = set.union(*[p[1] for p in possible])
# find which ingredients are always listed for a given allergen
possible = {
a:set.intersection(*[
s[1]
for s in possible
if s[0] == a]
) for a in allergens
}
# reduce
while any({i for i in possible if len(possible[i]) > 1}):
for b in {j for j in possible if len(possible[j]) > 1}:
for a in {i for i in possible if len(possible[i]) == 1}:
possible[b] -= possible[a]
# find sets of allergenic and non-allergenic
unsafe = set.union(*list(possible.values()))
safe = ingredients - unsafe
#
print('|non-allergen occurrences| =', sum(
food[0].split().count(s)
for food in foods
for s in safe)
)
print('allergens:', ','.join(
[
list(possible[a])[0]
for a in sorted(list(possible))
])
)
"""
all_ingredients = set()
all_allergens = set()
may_contain = {}
unsafe_ingredients = {}
foods = []
for line in lines:
ingredients, contains = line.split(' (contains ')
ingredients = ingredients.split(' ')
contains = contains[:-1].split(', ') # remove trailing ")" then split
all_ingredients = all_ingredients | set(ingredients)
all_allergens = all_allergens | set(contains)
"""
if __name__ == "__main__":
main()

22
2020/25/25.py Normal file
View File

@ -0,0 +1,22 @@
pubkey1 = 3418282
pubkey2 = 8719412
subject = 7
mod = 20201227
loop1 = 0
value1 = 1
while value1 != pubkey1:
value1 = (value1 * subject) % mod
loop1 += 1
loop2 = 0
value2 = 1
while value2 != pubkey2:
value2 = (value2 * subject) % mod
loop2 += 1
enckey1 = pow(pubkey2,loop1,mod)
enckey2 = pow(pubkey1,loop2,mod)
assert enckey1 == enckey2
print(enckey1)

2
2020/25/input.txt Normal file
View File

@ -0,0 +1,2 @@
3418282
8719412