From 68615a9ad2c942254135cffb00cf25a84a3b1356 Mon Sep 17 00:00:00 2001 From: Prefetch Date: Sat, 31 Dec 2022 22:21:39 +0100 Subject: Initial commit --- 08/main.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 08/main.py (limited to '08/main.py') 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() -- cgit v1.2.3