diff --git a/puzzles/day06.py b/puzzles/day06.py new file mode 100644 index 0000000..383181c --- /dev/null +++ b/puzzles/day06.py @@ -0,0 +1,79 @@ +""" +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() diff --git a/puzzles/test_day06.py b/puzzles/test_day06.py new file mode 100644 index 0000000..52ba7ea --- /dev/null +++ b/puzzles/test_day06.py @@ -0,0 +1,34 @@ +import pathlib +import pytest +import day06 as aoc + +INPUTS_DIR = f"{pathlib.Path(__file__).parent.parent}/inputs" + +@pytest.fixture +def example_data(): + input_path = f"{INPUTS_DIR}/day06-example.txt" + return aoc.parse(pathlib.Path(input_path).read_text().strip()) + + +@pytest.fixture +def day06_data(): + input_path = f"{INPUTS_DIR}/day06.txt" + return aoc.parse(pathlib.Path(input_path).read_text().strip()) + + +def test_example1(example_data): + assert aoc.part1(example_data) == 5934 + + +def test_example2(example_data): + assert aoc.part2(example_data) == 26984457539 + + +@pytest.mark.skip(reason="Not implemented") +def test_part1(day06_data): + assert aoc.part1(day06_data) == ... + + +@pytest.mark.skip(reason="Not implemented") +def test_part2(day06_data): + assert aoc.part2(day06_data) == ...