Solve 2021 day 6

This commit is contained in:
2024-11-17 06:11:54 -05:00
parent 074a7c4458
commit 11bcbce099
2 changed files with 34 additions and 1 deletions

32
2021/d6.py Normal file
View File

@@ -0,0 +1,32 @@
from lib import get_data
from lib import ints
from collections import defaultdict
data = get_data(__file__).strip()
xs = ints(data)
for _ in range(80):
for i in range(len(xs)):
if xs[i] == 0:
xs[i] = 6
xs.append(8)
else:
xs[i] -= 1
print(len(xs))
xs = ints(data)
fish = defaultdict(int)
for x in xs:
fish[x] += 1
for _ in range(256):
new_fish = defaultdict(int)
new_fish[8] = fish[0]
new_fish[6] = fish[0]
for i in range(1, 9):
new_fish[i - 1] += fish[i]
fish = new_fish
r = sum(fish.values())
print(r)