Advent of Code 2015 day 8 part2: solved
This commit is contained in:
parent
e2d962ead7
commit
73b8f691b2
|
|
@ -15,4 +15,21 @@ Santa's list is a file that contains many double-quoted string literals, one on
|
|||
|
||||
Disregarding the whitespace in the file, what is the number of characters of code for string literals minus the number of characters in memory for the values of the strings in total for the entire file?
|
||||
|
||||
For example, given the four strings above, the total number of characters of string code (2 + 5 + 10 + 6 = 23) minus the total number of characters in memory for string values (0 + 3 + 7 + 1 = 11) is 23 - 11 = 12.
|
||||
For example, given the four strings above, the total number of characters of string code (2 + 5 + 10 + 6 = 23) minus the total number of characters in memory for string values (0 + 3 + 7 + 1 = 11) is 23 - 11 = 12.
|
||||
|
||||
Your puzzle answer was 1371.
|
||||
|
||||
--- Part Two ---
|
||||
Now, let's go the other way. In addition to finding the number of characters of code, you should now encode each code representation as a new string and find the number of characters of the new encoded representation, including the surrounding double quotes.
|
||||
|
||||
For example:
|
||||
|
||||
"" encodes to "\"\"", an increase from 2 characters to 6.
|
||||
"abc" encodes to "\"abc\"", an increase from 5 characters to 9.
|
||||
"aaa\"aaa" encodes to "\"aaa\\\"aaa\"", an increase from 10 characters to 16.
|
||||
"\x27" encodes to "\"\\x27\"", an increase from 6 characters to 11.
|
||||
Your task is to find the total number of characters to represent the newly encoded strings minus the number of characters of code in each original string literal. For example, for the strings above, the total encoded length (6 + 9 + 16 + 11 = 42) minus the characters in the original code representation (23, just like in the first part of this puzzle) is 42 - 23 = 19.
|
||||
|
||||
Your puzzle answer was 2117.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||
|
|
@ -28,6 +28,25 @@ def calculate_discrepancy(unconverted_string: str) -> int:
|
|||
return (len(unconverted_string) - len(converted_string)) + 2
|
||||
|
||||
|
||||
def encode_longer(unencoded: str) -> int:
|
||||
encoded = ""
|
||||
for i, char in enumerate(unencoded):
|
||||
if char == '"':
|
||||
encoded += '\\"'
|
||||
elif char == '\\':
|
||||
encoded += char + char
|
||||
else:
|
||||
encoded += char
|
||||
return len(encoded) + 2 - len(unencoded)
|
||||
|
||||
|
||||
def encode_all(unconverted_strings: list) -> int:
|
||||
num_added_chars = 0
|
||||
for unconverted in unconverted_strings:
|
||||
num_added_chars += encode_longer(unconverted)
|
||||
return num_added_chars
|
||||
|
||||
|
||||
def calculate_all(unconverted_strings: list) -> int:
|
||||
number_discrepancy = 0
|
||||
for unconverted_string in unconverted_strings:
|
||||
|
|
@ -40,4 +59,4 @@ if __name__ == '__main__':
|
|||
print("solution: ", calculate_all(puzzle_input))
|
||||
|
||||
print("Part2: ")
|
||||
print("solution: ", )
|
||||
print("solution: ", encode_all(puzzle_input))
|
||||
|
|
|
|||
Loading…
Reference in New Issue