summaryrefslogtreecommitdiff
path: root/08/main.py
diff options
context:
space:
mode:
Diffstat (limited to '08/main.py')
-rwxr-xr-x08/main.py64
1 files changed, 64 insertions, 0 deletions
diff --git a/08/main.py b/08/main.py
new file mode 100755
index 0000000..bfd5b08
--- /dev/null
+++ b/08/main.py
@@ -0,0 +1,64 @@
+#!/usr/bin/python
+
+
+
+def solve_part1(lines):
+ result = 0
+
+ for line in lines:
+ len_code = len(line)
+
+ len_data = 0
+ i = 0
+ while i < len(line):
+ if line[i] == "\"":
+ pass
+ elif line[i] == "\\":
+ if line[i + 1] == "\"" or line[i + 1] == "\\":
+ len_data += 1
+ i += 1
+ elif line[i + 1] == "x":
+ len_data += 1
+ i += 3
+ else:
+ len_data += 1
+ i += 1
+
+ result += len_code - len_data
+
+ return result
+
+
+
+def solve_part2(lines_old):
+ # Escape all the provided strings once
+ lines_new = []
+ for old in lines_old:
+ new = "\""
+ for c in old:
+ if c == "\"":
+ new += "\\\""
+ elif c == "\\":
+ new += "\\\\"
+ else:
+ new += c
+ new += "\""
+ lines_new.append(new)
+
+ # Then we can just run part 1 on the new input
+ return solve_part1(lines_new)
+
+
+
+def main():
+ # Read (escaped) strings from input text file
+ with open("input.txt", "r") as f:
+ lines = f.read().splitlines()
+
+ print("Part 1 solution:", solve_part1(lines)) # 1333 for me
+ print("Part 2 solution:", solve_part2(lines)) # 2046 for me
+
+
+
+if __name__ == "__main__":
+ main()