summaryrefslogtreecommitdiff
path: root/08/main.py
diff options
context:
space:
mode:
authorPrefetch2022-12-31 22:21:39 +0100
committerPrefetch2022-12-31 22:21:39 +0100
commit68615a9ad2c942254135cffb00cf25a84a3b1356 (patch)
tree1ed3131f673207b2ef0bdaee3ee98bb68d6640ca /08/main.py
Initial commit
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()