Solve 2021:6 "Lanternfish" part 1

This commit is contained in:
Anders Englöf Ytterström 2021-12-06 07:15:37 +01:00
parent f82b6cd9b5
commit 2f3f892c0e
2 changed files with 59 additions and 0 deletions

View file

@ -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()

View file

@ -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()