2020 day 6 and 7

This commit is contained in:
felixm 2024-08-30 22:59:22 -04:00
parent 305fe0b325
commit 9a30c8b88d
3 changed files with 74 additions and 1 deletions

24
2020/d6.py Normal file
View File

@ -0,0 +1,24 @@
from lib import get_data
def part_1(data):
r1, r2 = 0, 0
for group in data.split("\n\n"):
r1 += len(set(group.replace("\n", "")))
s = set(group.splitlines()[0])
for line in group.splitlines():
s &= set(line)
r2 += len(s)
print(r1)
print(r2)
def main():
data = get_data(__file__)
part_1(data)
if __name__ == "__main__":
main()

47
2020/d7.py Normal file
View File

@ -0,0 +1,47 @@
from lib import get_data
def part_1(data):
bags = {}
for line in data.splitlines():
left, right = line.split(" contain ")
lb = " ".join(left.split()[:2])
rbs = right.replace(" bag", "").replace(" bags", "").replace(".", "").split(",")
if "no others" in rbs:
rbs = []
else:
rbs = [[int(v.split()[0]), " ".join(v.split()[1:])] for v in rbs]
assert lb not in bags
bags[lb] = rbs
for lb, rbs in bags.items():
for t in rbs:
if t[0] > 1:
t[1] = t[1][:-1]
can_hold = set(["shiny gold"])
can_hold_count = -1
while can_hold_count != len(can_hold):
can_hold_count = len(can_hold)
for lb, rbs in bags.items():
for _, bag_color in rbs:
if bag_color in can_hold:
can_hold.add(lb)
print(len(can_hold) - 1)
def count(color):
r = 1 # the bag itself
for c, icolor in bags[color]:
r += c * count(icolor)
return r
print(count("shiny gold") - 1)
def main():
data = get_data(__file__)
part_1(data)
if __name__ == "__main__":
main()

View File

@ -138,7 +138,9 @@ Solutions and utility script for Advent of Code challenges in Python.
- Day 3: 7:06 (way too slow, lol; time to take it seriously) - Day 3: 7:06 (way too slow, lol; time to take it seriously)
- Day 4: 14:30 (yo, I am just too slow) - Day 4: 14:30 (yo, I am just too slow)
- Day 5: 11:53 (not competitive) - Day 5: 11:53 (not competitive)
- Day 6: - Day 6: 4:11 (75th, finally on the leaderboard)
- Day 7: 24:39 (bad)
- Day 8:
## AoC 2022 ## AoC 2022