#!/usr/local/bin/python3

from ast import literal_eval    # Safer than plain eval

with open('../input/2015-08.txt') as f:
    strings = f.read().splitlines()

# Part one
total_symbols = sum(len(s) for s in strings)
total_chars = sum(len(literal_eval(s)) for s in strings)
print('Part one:', total_symbols - total_chars)

# Part two
# Instead of actually escaping the string using re.escape and measuring it,
# you can work out the number of extra characters needed by counting the
# characters that would need escaping. Add 2 for the extra surrounding quote
# marks mentioned in the problem description.
total_extra_chars = sum(s.count('"') + s.count('\\') + 2 for s in strings)
print('Part two:', total_extra_chars)

@@ -23,142 +23,931 @@
 n3%0A%0A
-from ast import literal_eval    # Safer than plain eval%0A%0Awith open('../input/2015-08.txt') as f:%0A    strings = f.read().splitlines()%0A%0A
+import re%0A%0A%0Adef parse_for_length(string):%0A    %22%22%22Return the length of string once escapes are parsed%22%22%22%0A    literal_length = len(string)%0A    # Account for and trim surrounding double quotes%0A    length = literal_length - 2%0A    string = string%5B1:-1%5D%0A    # Escaped backslashes%0A    length -= string.count(r'%5C%5C')%0A    # Escaped quotes%0A    length -= string.count(r'%5C%22')%0A    # Hex character escapes%0A    # (Times 3 because we want to reduce 4 chars to 1)%0A    hex_regex = re.compile(r'%5C%5Cx%5B0-9a-f%5D%7B2%7D', flags=re.I)%0A    length -= len(re.findall(hex_regex, string)) * 3%0A    return length%0A%0A%0Adef test_parse_for_length():%0A    %22%22%22parse_for_length returns expected lengths for known strings%22%22%22%0A    assert parse_for_length(r'%22%22') == 0%0A    assert parse_for_length(r'%22abc%22') == 3%0A    assert parse_for_length(r'%22aaa%5C%22aaa%22') == 7%0A    assert parse_for_length(r'%22%5Cx27%22') == 1%0A    assert parse_for_length(r'%22%5C%5C%22') == 1%0A%0A%0Adef main(puzzle_input):%0A    
 # Pa
@@ -953,16 +953,20 @@
 art one%0A
+    
 total_sy
@@ -1006,46 +1006,50 @@
 gs)%0A
-total_chars = sum(len(literal_eval
+    parsed_total = sum(parse_for_length
 (s)
-)
  for
@@ -1055,32 +1055,36 @@
 r s in strings)%0A
+    
 print('Part one:
@@ -1106,22 +1106,27 @@
 s - 
-total_chars)%0A%0A
+parsed_total)%0A%0A    
 # Pa
@@ -1132,16 +1132,20 @@
 art two%0A
+    
 # Instea
@@ -1212,16 +1212,20 @@
 ing it,%0A
+    
 # you ca
@@ -1289,16 +1289,20 @@
 ing the%0A
+    
 # charac
@@ -1336,17 +1336,17 @@
 ng. Add 
-2
+1
  for the
@@ -1367,16 +1367,20 @@
 ding
+%0A    #
  quote
-%0A#
  mar
@@ -1420,16 +1420,20 @@
 iption.%0A
+    
 total_ex
@@ -1479,17 +1479,17 @@
 '%5C%5C') + 
-2
+1
  for s i
@@ -1499,16 +1499,20 @@
 trings)%0A
+    
 print('P
@@ -1541,8 +1541,139 @@
 _chars)%0A
+%0A%0Aif __name__ == '__main__':%0A    with open('../input/2015-08.txt') as f:%0A        strings = f.read().splitlines()%0A    main(strings)%0A

Swap ast_eval for own parsing, add test