summary refs log tree commit diff stats
path: root/day8.py
diff options
context:
space:
mode:
authorBrian Chu <brianmchu42@gmail.com>2021-12-30 15:11:21 -0800
committerBrian Chu <brianmchu42@gmail.com>2021-12-30 15:11:21 -0800
commite7085453864431ace3ad8f3123b259ed0829ae74 (patch)
tree2ef1fbb0e9d02fc934b5e09d96dd187f3e371ea6 /day8.py
downloadAdventOfCode2015-e7085453864431ace3ad8f3123b259ed0829ae74.tar.gz
all solutions for 2015 main
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)