-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaoc202106.py
66 lines (46 loc) · 1.36 KB
/
aoc202106.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""AoC 6, 2021: Lanternfish."""
# Standard library imports
import collections
import functools
import pathlib
import sys
def parse_data(puzzle_input):
"""Parse input."""
return collections.Counter(int(timer) for timer in puzzle_input.split(","))
def part1(data):
"""Solve part 1."""
return count_lanternfish(data, 80)
def part2(data):
"""Solve part 2."""
return count_lanternfish(data, 256)
def count_lanternfish(fish_count, days):
"""Count the total number of lanternfish after the given number of days."""
return sum(
num_fish * lanternfish(days - timer) for timer, num_fish in fish_count.items()
)
@functools.cache
def lanternfish(days):
"""Calculate how many lanternfish one lanternfish create in a given number
of days.
>>> lanternfish(0)
1
>>> lanternfish(1)
2
>>> lanternfish(16)
5
>>> lanternfish(17)
7
"""
if days < 1:
return 1
return lanternfish(days - 7) + lanternfish(days - 9)
def solve(puzzle_input):
"""Solve the puzzle for the given input."""
data = parse_data(puzzle_input)
yield part1(data)
yield part2(data)
if __name__ == "__main__":
for path in sys.argv[1:]:
print(f"\n{path}:")
solutions = solve(puzzle_input=pathlib.Path(path).read_text().strip())
print("\n".join(str(solution) for solution in solutions))