summaryrefslogtreecommitdiff
path: root/2024/3B.py
diff options
context:
space:
mode:
Diffstat (limited to '2024/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)