#Skip to menu

Day 8, Year 2015: Matchsticks

First read the problem description.
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
import import_ipynb
import helper
s = helper.read_file('2015_8.txt')
cc, cm, ce = count_characters(s)
print(cc - cm)
print(ce - cc)
1371
2117

Source code of the solution(s):