summary refs log tree commit diff stats
path: root/day8.py
diff options
context:
space:
mode:
Diffstat (limited to 'day8.py')
-rw-r--r--day8.py34
1 files changed, 34 insertions, 0 deletions
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)