My advent of code solutions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

79 lines
1.8 KiB

"""
Advent of Code 2021 - day 06
Run with:
python puzzles/day06.py inputs/day06.txt
"""
import pathlib
import sys
from dataclasses import dataclass
from typing import List, Tuple
@dataclass
class LanternFish:
reproduce_timer: int
children: int = 0
class FishSchool:
fish = None
def __init__(self, times: List[int]) -> None:
self.fish = []
for time in times:
self.fish.append(LanternFish(reproduce_timer=time))
def simulate(self, days: int=80) -> None:
for day in range(days):
self._check_school()
def _check_school(self) -> None:
for index, fish in enumerate(self.fish):
if fish.reproduce_timer == 0:
self.fish.append(LanternFish(reproduce_timer=9)) # Need to correct (should be 8)
self.fish[index].reproduce_timer = 6
self.fish[index].children += 1
else:
self.fish[index].reproduce_timer -= 1
def part1(inputs: List[int]) -> int:
school = FishSchool(inputs)
school.simulate(days=80)
return len(school.fish)
def part2(inputs: List[int]) -> int:
school = FishSchool(inputs)
school.simulate(days=256)
return len(school.fish)
def parse(inputs: str) -> List[int]:
"""Parse the input string"""
return [int(value) for value in inputs.split(",")]
def solve(path: str) -> Tuple[int, int]:
"""Solve the puzzle"""
puzzle_input = parse(pathlib.Path(path).read_text().strip())
part1_result = part1(puzzle_input)
part2_result = part2(puzzle_input)
return part1_result, part2_result
def main() -> None:
for path in sys.argv[1:]:
print(f"Input File: {path}")
part1_result, part2_result = solve(path)
print(f"Part 1 Result: {part1_result}")
print(f"Part 2 Result: {part2_result}")
if __name__ == "__main__":
main()