summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFanDabbyDoozy <ps20163152@student.vsa.edu.hk>2024-12-08 11:05:01 +0800
committerGitHub <noreply@github.com>2024-12-08 11:05:01 +0800
commit8128b4edcaca761585f6ab81c7c4a9aaff763532 (patch)
tree78027a4067d74352ef15fafc1abe52436b9dae3f
parentdc976d3cba3652037991b3b000c752e9dda83f2b (diff)
Create 3B.py
-rw-r--r--2024/3B.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/2024/3B.py b/2024/3B.py
new file mode 100644
index 0000000..61852d0
--- /dev/null
+++ b/2024/3B.py
@@ -0,0 +1,41 @@
+import re
+
+with open('3.txt', 'r') as f:
+ data = f.read()
+# Regular expressions for matching patterns
+mul_pattern = r'mul\(\d+,\d+\)'
+do_pattern = r'do\(\)'
+dont_pattern = r"don't\(\)"
+
+multiply = True
+muls = 0
+
+en_muls = []
+
+# Find all mul commands and do/don't instructions
+matches = re.finditer(mul_pattern + '|' + do_pattern + '|' + dont_pattern, data)
+
+for match in matches:
+ pattern = match.group()
+ print(pattern)
+
+ #identify "do()" and "dont()"
+
+ if pattern == "do()":
+ multiply = True
+ elif pattern == "don't()":
+ multiply = False
+ elif pattern.startswith('mul'):
+ if multiply:
+ en_muls.append(pattern)
+
+print(en_muls)
+
+ints = [l.split("mul(")[1].rstrip(')') for l in en_muls]
+print(ints)
+
+for i in ints:
+ n1, n2 = map(int, i.split(','))
+ print(n1, n2)
+ muls += n1 * n2
+print(muls)