#!/usr/bin/env python # coding: utf-8 # In[65]: from enum import Enum, auto class State(Enum): StartString = auto() ParseString = auto() EscapeCharacter = auto() EscapeHexadecimal = auto() def count_characters(s): cc = 0 cm = 0 ce = 0 state = State.StartString hexadecimal_count = 0 for i in range(len(s)): if s[i].isspace(): continue cc += 1 ce += 1 if s[i] in ('"', '\\'): ce += 1 if state is State.StartString: if s[i] != '"': raise Exception('expecting "') ce += 2 state = State.ParseString continue if state is State.ParseString: if s[i] == '\\': state = State.EscapeCharacter continue if s[i] == '"': state = State.StartString continue cm += 1 if state is State.EscapeCharacter: if s[i] == 'x': state = State.EscapeHexadecimal hexadecimal_count = 0 continue else: state = State.ParseString cm += 1 if state is State.EscapeHexadecimal: hexadecimal_count += 1 if hexadecimal_count == 2: state = State.ParseString cm += 1 return cc, cm, ce # In[66]: import import_ipynb import helper s = helper.read_file('2015_8.txt') # In[67]: cc, cm, ce = count_characters(s) print(cc - cm) print(ce - cc) # In[ ]: