From 091ef712aafb939bdfe7b71440a2b03481ac7951 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Wed, 8 Nov 2023 11:15:21 -0800 Subject: [PATCH] day8 --- README.md | 4 ++++ tests/y_2015/test_2015_day8.py | 18 ++++++++++++++++++ y_2015/day8.py | 17 +++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 tests/y_2015/test_2015_day8.py create mode 100644 y_2015/day8.py diff --git a/README.md b/README.md index c521fe5..5b97a76 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,10 @@ [201505]: https://github.com/Stegallo/adventofcode/blob/master/y_2015/day5.py [201505p]: https://adventofcode.com/2015/day/5 + +[201508]: https://github.com/Stegallo/adventofcode/blob/master/y_2015/day8.py +[201508p]: https://adventofcode.com/2015/day/8 + ## to run the code activate the adventofcode virtual environment: diff --git a/tests/y_2015/test_2015_day8.py b/tests/y_2015/test_2015_day8.py new file mode 100644 index 0000000..18499df --- /dev/null +++ b/tests/y_2015/test_2015_day8.py @@ -0,0 +1,18 @@ +from __future__ import annotations + +from unittest.mock import mock_open, patch + +from y_2015.day8 import Day + +with patch("builtins.open", mock_open(read_data="")): + day = Day() + + +def test_calculate_1(): + day._Day__input_data = ['""', '"abc"', '"aaa\\"aaa"', '"\\x27"'] + assert day._calculate_1() == 12 + + +def test_calculate_2(): + day._Day__input_data = ['""', '"abc"', '"aaa\\"aaa"', '"\\x27"'] + assert day._calculate_2() == 19 diff --git a/y_2015/day8.py b/y_2015/day8.py new file mode 100644 index 0000000..964ef3e --- /dev/null +++ b/y_2015/day8.py @@ -0,0 +1,17 @@ +from common.aoc import AoCDay + + +class Day(AoCDay): + def __init__(self, test=0): + super().__init__(__name__, test) + + def _preprocess_input(self): + self.__input_data = self._input_data[0] + + def _calculate_1(self): + string_len = sum(len(x) for x in self.__input_data) + mem_len = sum(len(eval(x)) for x in self.__input_data) + return string_len - mem_len + + def _calculate_2(self): + return sum(2 + x.count("\\") + x.count('"') for x in self.__input_data)