From 4742910afd4464a000a153bc78344079436dc3a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Wed, 25 Oct 2023 23:26:51 +0200 Subject: [PATCH] Solve 2015:10 "Elves Look, Elves Say" --- 2015-python/solutions/day_10.py | 39 ++++++++++++++++++++++++++++++++ 2015-python/tests/test_day_10.py | 31 +++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 2015-python/solutions/day_10.py create mode 100644 2015-python/tests/test_day_10.py diff --git a/2015-python/solutions/day_10.py b/2015-python/solutions/day_10.py new file mode 100644 index 0000000..fedf388 --- /dev/null +++ b/2015-python/solutions/day_10.py @@ -0,0 +1,39 @@ +from itertools import chain +from solutions import BaseSolution + + +class Solution(BaseSolution): + input_file = "10.txt" + + def __str__(self): + return "Day 10: Elves Look, Elves Say" + + def parse_input(self, data): + return data.strip() + + def solve(self, puzzle_input, iterations=40): + return len(self._sequence(puzzle_input, iterations)) + + def solve_again(self, puzzle_input, iterations=50): + return len(self._sequence(puzzle_input, iterations)) + + def _sequence(self, sequence, iterations=40): + for _ in range(iterations): + chunks = [] + v = sequence[0] + counter = 0 + for c in sequence: + if c == v: + counter += 1 + else: + chunks.append([str(counter), v]) + v = c + counter = 1 + chunks.append([str(counter), v]) + sequence = "".join(chain(*chunks)) + return sequence + + +if __name__ == "__main__": + solution = Solution() + solution.show_results() diff --git a/2015-python/tests/test_day_10.py b/2015-python/tests/test_day_10.py new file mode 100644 index 0000000..db0cd85 --- /dev/null +++ b/2015-python/tests/test_day_10.py @@ -0,0 +1,31 @@ +import unittest + +from solutions.day_10 import Solution + + +class Day10TestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + self.puzzle_input = self.solution.parse_input( + """ + + """ + ) + + def test_parse_puzzle_input(self): + data = """ + 12 + """ + assert self.solution.parse_input(data) == "12" + + def test_solve_first_part(self): + assert self.solution._sequence("1", 1) == "11" + assert self.solution._sequence("11", 1) == "21" + assert self.solution._sequence("21", 1) == "1211" + assert self.solution._sequence("1211", 1) == "111221" + assert self.solution._sequence("111221", 1) == "312211" + assert self.solution._sequence("1", 5) == "312211" + + +if __name__ == "__main__": + unittest.main()