Skip to content

Commit

Permalink
Comments and less extreme code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
micknudsen committed Dec 3, 2023
1 parent 40c51c6 commit 4201170
Showing 1 changed file with 18 additions and 50 deletions.
68 changes: 18 additions & 50 deletions 2015/08/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,35 @@
def overhead(
string: str,
) -> int:
"""Every line on Santa's list is enclosed by double quotes and
maybe contain some escaped characters. This function computes the
overhead memory used for storing all this things compared to the
bare string literal itself."""
return len(string) - len(ast.literal_eval(string))


def increase(
string: str,
) -> int:
"""Every entry on Santa's list must now be further encoded. This
means enclosing the string literal in double quotes and escaping
quotes and backslashes inside the string itself. This function
computes the increase in memory usage for storing all these things."""
return 2 + string.count('"') + string.count("\\")


class TestCode(unittest.TestCase):
def test_overhead(self) -> None:
self.assertEqual(
overhead(string=r'""'),
2,
)
self.assertEqual(
overhead(string=r'"abc"'),
2,
)
self.assertEqual(
overhead(string=r'"aaa\"aaa"'),
3,
)
self.assertEqual(
overhead(string=r'"\x27"'),
5,
)
self.assertEqual(overhead(string=r'""'), 2)
self.assertEqual(overhead(string=r'"abc"'), 2)
self.assertEqual(overhead(string=r'"aaa\"aaa"'), 3)
self.assertEqual(overhead(string=r'"\x27"'), 5)

def test_increase(self) -> None:
self.assertEqual(
increase(string=r'""'),
4,
)
self.assertEqual(
increase(string=r'"abc"'),
4,
)
self.assertEqual(
increase(string=r'"aaa\"aaa"'),
6,
)
self.assertEqual(
increase(string=r'"\x27"'),
5,
)
self.assertEqual(increase(string=r'""'), 4)
self.assertEqual(increase(string=r'"abc"'), 4)
self.assertEqual(increase(string=r'"aaa\"aaa"'), 6)
self.assertEqual(increase(string=r'"\x27"'), 5)


class TestPuzzles(unittest.TestCase):
Expand All @@ -58,23 +42,7 @@ def setUp(self) -> None:
self.strings = f.read().splitlines()

def test_part_one(self) -> None:
self.assertEqual(
sum(
map(
overhead,
self.strings,
)
),
1333,
)
self.assertEqual(sum(map(overhead, self.strings)), 1333)

def test_part_two(self) -> None:
self.assertEqual(
sum(
map(
increase,
self.strings,
)
),
2046,
)
self.assertEqual(sum(map(increase, self.strings)), 2046)

0 comments on commit 4201170

Please sign in to comment.