Skip to content

Commit

Permalink
[year2015/day8] Part Two
Browse files Browse the repository at this point in the history
  • Loading branch information
UnicodeTreason committed Jan 3, 2025
1 parent 4f8feb9 commit 139cec3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
51 changes: 41 additions & 10 deletions adventofcode/year2015/day08.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


# --- Day 8: Matchsticks ---
def part_one(input_data: list) -> dict:
def part_one(input_data: list) -> int:
"""Calculate
Parameters
Expand All @@ -27,8 +27,6 @@ def part_one(input_data: list) -> dict:
# Process Input
for escaped_string in input_data:
# Count of characters at the code level
print(f'STR: {escaped_string}')
print(f'LEN: {len(escaped_string)}')
characters_code += len(escaped_string)

# Count of characters at the memory level
Expand All @@ -41,16 +39,49 @@ def part_one(input_data: list) -> dict:
escaped_string = re.sub(r'\\x([0-9]|[a-f]){2}', 'X', escaped_string)
# Replace escaped backslash with backslash
escaped_string = escaped_string.replace(r'\\', '\\')
# Add count
characters_memory += len(escaped_string)
print(f'STR: {escaped_string}')
print(f'LEN: {len(escaped_string)}')
print('')

print(f'code: {characters_code}')
print(f'memory: {characters_memory}')
return characters_code - characters_memory


def part_two(input_data: list) -> int:
"""Calculate
Parameters
----------
input_data: list
List of strings containing the following elements:
double-quoted string literals: "abc"
escape sequences:
\\ (which represents a single backslash)
\" (which represents a lone double-quote character)
\ x plus two hexadecimal characters (which represents a single character with that ASCII code)
Returns
-------
characters_recoded - characters_code: int
"""

characters_recoded = 0
characters_code = 0

# Process Input
for escaped_string in input_data:
# Count of characters at the code level
characters_code += len(escaped_string)

# Count of characters after recoding
# Replace backslash with escaped backslash
escaped_string = escaped_string.replace('\\', r'\\')
# Replace quote with escaped quote
escaped_string = escaped_string.replace(r'"', r'\"')
# Add outside quotes
escaped_string = '"' + escaped_string + '"'
# Add count
characters_recoded += len(escaped_string)
return characters_recoded - characters_code


if __name__ == "__main__":
# Read input from disk cache
path_inputs = Path(__file__).parent / 'inputs' / f'{Path(__file__).stem}_input.txt'
Expand All @@ -61,4 +92,4 @@ def part_one(input_data: list) -> dict:

print('Calculating Solutions...')
print(f'Part 01: {part_one(input_data)}')
# print(f'Part 02: {part_two(input_data)}')
print(f'Part 02: {part_two(input_data)}')
9 changes: 8 additions & 1 deletion tests/year2015/test_day08.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import pytest

from adventofcode.year2015.day08 import part_one
from adventofcode.year2015.day08 import part_one, part_two


@pytest.mark.parametrize('test_input, expected', [
([r'""', r'"abc"', r'"aaa\"aaa"', r'"\x27"'], 12),
])
def test_part_one(test_input, expected):
assert part_one(test_input) == expected


@pytest.mark.parametrize('test_input, expected', [
([r'""', r'"abc"', r'"aaa\"aaa"', r'"\x27"'], 19),
])
def test_part_two(test_input, expected):
assert part_two(test_input) == expected

0 comments on commit 139cec3

Please sign in to comment.