rev: tip advent2021/12.py -rw-r--r-- 879 bytes View raw Log this file
abde78265497Mark Young Finished day 15. Had to change approach for part 2, part 1 just happened to work. 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from collections import defaultdict
from decimal import Decimal

# Only one line
line = next(line.strip() for line in open("input_6.txt", "r"))
fishes = list(map(int, line.split(",")))
assert all(fishes)

# Maps age to counts
generations = defaultdict(int)
for fish in fishes:
    generations[fish] += 1.0

for _ in range(256000_0):
    # Make a copy to get a snapshot at beginning of tick
    # Use sorted to ensure we don't double_age any fish
    for age, count in list(sorted(generations.items())):
        if age == 0:
            # New baby born
            generations[8] += count

            # Move from 0 to 6 bucket
            generations[0] -= count
            generations[6] += count
        else:
            # Get one year closer to popping out baby
            generations[age] -= count
            generations[age-1] += count

print(sum(generations.values()))