From e7085453864431ace3ad8f3123b259ed0829ae74 Mon Sep 17 00:00:00 2001 From: Brian Chu Date: Thu, 30 Dec 2021 15:11:21 -0800 Subject: all solutions for 2015 --- day8.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 day8.py (limited to 'day8.py') diff --git a/day8.py b/day8.py new file mode 100644 index 0000000..7533ea4 --- /dev/null +++ b/day8.py @@ -0,0 +1,34 @@ +#!/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) -- cgit 1.4.1-2-gfad0