diff --git a/adventofcode/year2015/day08.py b/adventofcode/year2015/day08.py index 6f25e3f..c2d61df 100644 --- a/adventofcode/year2015/day08.py +++ b/adventofcode/year2015/day08.py @@ -3,7 +3,7 @@ # --- Day 8: Matchsticks --- -def part_one(input_data: list) -> dict: +def part_one(input_data: list) -> int: """Calculate Parameters @@ -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 @@ -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' @@ -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)}') diff --git a/tests/year2015/test_day08.py b/tests/year2015/test_day08.py index 7bd4472..e99f67b 100644 --- a/tests/year2015/test_day08.py +++ b/tests/year2015/test_day08.py @@ -1,6 +1,6 @@ import pytest -from adventofcode.year2015.day08 import part_one +from adventofcode.year2015.day08 import part_one, part_two @pytest.mark.parametrize('test_input, expected', [ @@ -8,3 +8,10 @@ ]) 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