Solve 2015:10 "Elves Look, Elves Say"

This commit is contained in:
Anders Englöf Ytterström 2023-10-25 23:26:51 +02:00
parent 84d33a8b26
commit 4742910afd
2 changed files with 70 additions and 0 deletions

View file

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

View file

@ -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(
"""
<REPLACE ME>
"""
)
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()