text: import string
  
  def is_string_all_hex(line):
      return all(char in string.hexdigits for char in line)
  
  def decode(line):
      while True:
          pos = line.find("\\") # position of 1st esc sequence
          if "\\x" == line[pos:pos+2]: # \x{num}{num} == 1 char
              if is_string_all_hex(line[pos+2:pos+4]):
                  line = line[:pos] + "$" + line[pos + 4:]
              else:
                  line = line[:pos - 1] + "x" + line[pos:]
          elif "\\\\" == line[pos:pos+2]: # \\ == 1 backslash
              line = line.replace("\\\\","!", 1)
          elif "\\\"" == line[pos:pos+2]: # \"" == 1 double quote
              line = line.replace("\\\"","@", 1)
          else: # remove edge " and exit loop
              line = line.replace("\"","")
              break
  
      return line
  
  def part1(lines):
      total = 0
      for line in lines:
          total += len(line) # original length
          line = decode(line)
          total -= len(line)
      return total
  
  def encode(line):
      while True:
          pos = line.find("\\") # position of 1st esc sequence
          if "\\x" == line[pos:pos+2]: 
              if is_string_all_hex(line[pos+2:pos+4]):
                  line = line[:pos] + "$$$$$" + line[pos + 4:]
              else:
                  line = line[:pos - 1] + "x" + line[pos:]
          elif "\\\\" == line[pos:pos+2]:
              line = line.replace("\\\\","!!!!", 1)
          elif "\\\"" == line[pos:pos+2]:
              line = line.replace("\\\"","@@@@", 1)
          else: # encode edge " and exit loop
              line = "\"" +line.replace("\"","##") + "\""
              break
  
      return line
  
  def part2(lines):
      total = 0
      for line in lines:
          total -= len(line) # original length
          line = encode(line)
          total += len(line)
      return total
  
  if __name__ == "__main__":
      with open("../input.txt") as file:
          lines = [line.strip() for line in file]
  
      print(part1(lines))
      print(part2(lines))