Skip to content

Commit f7d87d5

Browse files
authored
Create task2.py
1 parent 2574faa commit f7d87d5

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

day3/task2.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Parse the corrupted memory to handle do() and don't() instructions
2+
enabled = True # Start with mul instructions enabled
3+
total_sum = 0
4+
5+
# Linear scan through the file
6+
tokens = re.split(r"(\bdo\(\)|\bdon't\(\)|mul\(\d{1,3},\d{1,3}\))", data)
7+
8+
# Process each token
9+
for token in tokens:
10+
if token == "do()":
11+
enabled = True
12+
elif token == "don't()":
13+
enabled = False
14+
elif token.startswith("mul(") and enabled:
15+
# Extract numbers from the mul() instruction
16+
match = re.match(r"mul\((\d{1,3}),(\d{1,3})\)", token)
17+
if match:
18+
x, y = int(match[1]), int(match[2])
19+
total_sum += x * y
20+
21+
total_sum

0 commit comments

Comments
 (0)