Solve 2015:8 "Machinesticks"
This commit is contained in:
parent
2dff9463ed
commit
5aba2fbed8
2 changed files with 57 additions and 0 deletions
31
2015-python/solutions/day_08.py
Normal file
31
2015-python/solutions/day_08.py
Normal file
|
|
@ -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()
|
||||||
26
2015-python/tests/test_day_08.py
Normal file
26
2015-python/tests/test_day_08.py
Normal file
|
|
@ -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()
|
||||||
Loading…
Add table
Reference in a new issue