summary refs log blame commit diff stats
path: root/day8.py
blob: 7533ea4e6a6bbd8d1f5fa0e55def56555b06efe4 (plain) (tree)

































                                             
#!/usr/bin/env python

import re

with open('day8.txt') as data:
    strings = [line.strip() for line in data]

# part 1
total = 0
for string in strings:
    orig_len = len(string)
    eval_len = len(eval(string))
    total += orig_len - eval_len

print(total)

# part 2
def encode(s):
    result = ''
    for c in s:
        if c == '"':
            result += '\\\"'
        elif c == '\\':
            result += '\\\\'
        else:
            result += c
    return '"' + result + '"'

total = 0
for string in strings:
    encoded = encode(string)
    total += len(encoded) - len(string)

print(total)