From 63eeefc68b813d4bda2b49737b896e81f8c934e6 Mon Sep 17 00:00:00 2001
From: zb5g22 <zb5g22@soton.ac.uk>
Date: Tue, 3 Dec 2024 10:23:52 +0000
Subject: [PATCH] Upload day 3 part b

---
 Day 3/d3b.py | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
 create mode 100644 Day 3/d3b.py

diff --git a/Day 3/d3b.py b/Day 3/d3b.py
new file mode 100644
index 0000000..ad3805e
--- /dev/null
+++ b/Day 3/d3b.py	
@@ -0,0 +1,31 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Tue Dec  3 09:37:50 2024
+
+@author: zoë
+"""
+
+import re
+
+
+with open("input", "r") as file:
+    data = file.readlines()
+
+
+regex = re.compile(r"mul\([0-9]+,[0-9]+\)|do\(\)|don't\(\)")
+
+enabled = 1
+product_sum = 0
+for row in data:
+    valid_sequences = re.findall(regex, row)
+    for item in valid_sequences:
+        if item[2] == "(":
+            enabled = 1
+        elif item[2] == "n":
+            enabled = 0
+        else:
+            nums = item.strip(")").strip("mul(").split(",")
+            product = int(nums[0]) * int(nums[1]) * enabled
+            product_sum += product
+
+print(f"Sum of all products: {product_sum}")
-- 
GitLab