diff --git a/2021-python/solutions/day_09.py b/2021-python/solutions/day_09.py new file mode 100644 index 0000000..451805c --- /dev/null +++ b/2021-python/solutions/day_09.py @@ -0,0 +1,34 @@ +from solutions import BaseSolution + + +class Solution(BaseSolution): + input_file = "09.txt" + + def __str__(self): + return "Day 9: Smoke Basin" + + def parse_input(self, data): + return [[int(c) for c in r] for r in data.split()] + + def solve(self, puzzle_input): + lp = 0 + lpi = len(puzzle_input) + for v, row in enumerate(puzzle_input): + lr = len(row) + for i in range(lr): + x = row[i] + s1 = row[i - 1] if i > 0 else 11 + s2 = row[i + 1] if i < lr - 1 else 11 + s3 = puzzle_input[v - 1][i] if v > 0 else 11 + s4 = puzzle_input[v + 1][i] if v < lpi - 1 else 11 + if all(x < s for s in [s1, s2, s3, s4]): + lp += 1 + x + return lp + + def solve_again(self, puzzle_input): + return True + + +if __name__ == "__main__": + solution = Solution() + solution.show_results() diff --git a/2021-python/tests/test_day_09.py b/2021-python/tests/test_day_09.py new file mode 100644 index 0000000..69e0225 --- /dev/null +++ b/2021-python/tests/test_day_09.py @@ -0,0 +1,37 @@ +import unittest + +from solutions.day_09 import Solution + + +class Day09TestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + self.puzzle_input = self.solution.parse_input( + """ + 2199943210 +3987894921 +9856789892 +8767896789 +9899965678 + """ + ) + + def test_parse_puzzle_input(self): + data = """ + 2199943210 +3987894921 + """ + assert self.solution.parse_input(data) == [ + [2, 1, 9, 9, 9, 4, 3, 2, 1, 0], + [3, 9, 8, 7, 8, 9, 4, 9, 2, 1], + ] + + def test_solve_first_part(self): + assert self.solution.solve(self.puzzle_input) == 15 + + # def test_solve_second_part(self): + # assert self.solution.solve_again(self.puzzle_input) == True + + +if __name__ == "__main__": + unittest.main()