Skip to content

Commit 9abe027

Browse files
committed
Completed day 4 of year 2024
1 parent 47cc96a commit 9abe027

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

2024/3.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import re
5+
6+
split_data = False
7+
completed = True
8+
raw_data = None # Not To be touched
9+
10+
11+
def part1(data):
12+
mul = re.findall(r'mul\((\d{1,3}),(\d{1,3})\)', data)
13+
cum = 0
14+
for num1, num2 in mul:
15+
cum += int(num1) * int(num2)
16+
17+
return cum
18+
19+
def part2(data):
20+
compiled = re.compile(r"mul\((\d{1,3}),(\d{1,3})\)|do\(\)|don't\(\)")
21+
22+
ignore = False
23+
cum = 0
24+
for out in compiled.finditer(data):
25+
if out.group() == 'do()':
26+
ignore = False
27+
elif out.group() == "don't()":
28+
ignore = True
29+
elif not ignore:
30+
cum += int(out.group(1)) * int(out.group(2))
31+
32+
return cum

0 commit comments

Comments
 (0)