diff --git a/2021-python/solutions/day_06.py b/2021-python/solutions/day_06.py new file mode 100644 index 0000000..d8d160c --- /dev/null +++ b/2021-python/solutions/day_06.py @@ -0,0 +1,30 @@ +from solutions import BaseSolution + + +class Solution(BaseSolution): + input_file = "06.txt" + + def __str__(self): + return "Day 6: Lanternfish" + + def parse_input(self, data): + return list(map(int, data.strip().split(","))) + + def solve(self, puzzle_input): + return self._produce(puzzle_input, 80) + + def solve_again(self, puzzle_input): + return self._produce(puzzle_input, 256) + + def _produce(self, puzzle_input, tf): + f = [*puzzle_input] + for _ in range(tf): + n = [8 for __ in range(sum(x <= 0 and x % 7 == 0 for x in f))] + f = list(map(lambda x: x - 1, f)) + f.extend(n) + return len(f) + + +if __name__ == "__main__": + solution = Solution() + solution.show_results() diff --git a/2021-python/tests/test_day_06.py b/2021-python/tests/test_day_06.py new file mode 100644 index 0000000..7b4fd3d --- /dev/null +++ b/2021-python/tests/test_day_06.py @@ -0,0 +1,29 @@ +import unittest + +from solutions.day_06 import Solution + + +class Day06TestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + self.puzzle_input = self.solution.parse_input( + """ + 3,4,3,1,2 + """ + ) + + def test_parse_puzzle_input(self): + data = """ + 3,4,3,1,2 + """ + assert self.solution.parse_input(data) == [3, 4, 3, 1, 2] + + def test_solve_first_part(self): + assert self.solution.solve(self.puzzle_input) == 5934 + + # def test_solve_second_part(self): + # assert self.solution.solve_again(self.puzzle_input) == 26984457539 + + +if __name__ == "__main__": + unittest.main()