import time import codecs start = time.time() file = codecs.open('input.txt', encoding='ascii') count = {'literals': 0, 'representations': 0, 'escaped': 0} for line in file: line = line.strip() count['literals'] += len(line) count['representations'] += len(codecs.decode(line.encode('ascii'), 'unicode_escape'))-2 count['escaped'] += len(line.encode('unicode_escape').replace('"', '\\"'))+2 print("Part 1: " + str(count['literals'] - count['representations']) + " Part 2: " + str(count['escaped'] - count['literals'])) print(time.time() - start)