thiagowfx's avatar

Β¬ just serendipity πŸ€ (not just serendipity)

Advent of Code 2021: Day 6

β€’ 93 words β€’ 1 min β€’ updated

⚠️ This post is over one year old. It may no longer be up to date or relevant. Opinions may have changed.

Refer to the previous post about AoC, and to the git repository with my solutions in Python 3.

Link to Day #6 puzzle.

Simulate lanternfish population growth using a counter to track fish by their internal timer.

python
#!/usr/bin/env python3
from collections import Counter
import sys

with open(sys.argv[1]) as input:
    numbers = list(map(int, input.read().split(',')))

def simulation(days):
    fish = Counter(numbers)

    for _ in range(days):
        next_fish = Counter()
        for timer, count in fish.items():
            if timer == 0:
                next_fish[8] += fish[timer]
                next_fish[6] += fish[timer]
            else:
                next_fish[timer - 1] += fish[timer]
        fish = next_fish

    print(sum(fish.values()))

simulation(80)
simulation(256)