diff --git a/2015-python/solutions/day_08.py b/2015-python/solutions/day_08.py new file mode 100644 index 0000000..60f5109 --- /dev/null +++ b/2015-python/solutions/day_08.py @@ -0,0 +1,31 @@ +import re +from solutions import BaseSolution + + +class Solution(BaseSolution): + input_file = "08.txt" + + def __str__(self): + return "Day 8: Matchsticks" + + def parse_input(self, data): + return data.split() + + def solve(self, puzzle_input): + def lendiff(line): + readable = eval(line) + return len(line) - len(readable) + + return sum(map(lendiff, puzzle_input)) + + def solve_again(self, puzzle_input): + def lendiff(line): + encoded = f'"{line.replace("\\", "\\\\").replace('"', '\\"')}"' + return len(encoded) - len(line) + + return sum(map(lendiff, puzzle_input)) + + +if __name__ == "__main__": + solution = Solution() + solution.show_results() diff --git a/2015-python/tests/test_day_08.py b/2015-python/tests/test_day_08.py new file mode 100644 index 0000000..e5e4e7b --- /dev/null +++ b/2015-python/tests/test_day_08.py @@ -0,0 +1,26 @@ +import unittest + +from solutions.day_08 import Solution + + +class Day08TestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + self.puzzle_input = self.solution.parse_input( + r""" +"" +"abc" +"aaa\"aaa" +"\x27" + """ + ) + + def test_solve_first_part(self): + assert self.solution.solve(self.puzzle_input) == 12 + + def test_solve_second_part(self): + assert self.solution.solve_again(self.puzzle_input) == 19 + + +if __name__ == "__main__": + unittest.main()