Skip to content

Commit e861019

Browse files
committed
2024 d3: Add original and clean solution
1 parent e04b007 commit e861019

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

2024/original_solutions/day03.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python3
2+
3+
from utils.all import *
4+
5+
advent.setup(2024, 3)
6+
7+
fin = advent.get_input()
8+
data = fin.read()
9+
10+
ans1 = ans2 = 0
11+
12+
for a, b in re.findall(r'mul\((\d+),(\d+)\)', data):
13+
a = int(a)
14+
b = int(b)
15+
ans1 += a * b
16+
17+
advent.print_answer(1, ans1)
18+
19+
enabled = True
20+
21+
for m in re.findall(r'mul\((\d+),(\d+)\)|(don\'t\(\))|(do\(\))', data):
22+
if m[2] == 'don\'t()':
23+
enabled = False
24+
continue
25+
elif m[3] == 'do()':
26+
enabled = True
27+
continue
28+
29+
if enabled:
30+
a, b = m[:2]
31+
a = int(a)
32+
b = int(b)
33+
ans2 += a * b
34+
35+
advent.print_answer(2, ans2)

2024/solutions/day03.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
from re import findall
5+
6+
# Open the first argument as input or use stdin if no arguments were given
7+
fin = open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin
8+
9+
total1 = total2 = 0
10+
enabled = True
11+
12+
for a, b, do, dont in findall(r"mul\((\d+),(\d+)\)|(do\(\))|(don't\(\))", fin.read()):
13+
if do or dont:
14+
enabled = bool(do)
15+
else:
16+
x = int(a) * int(b)
17+
total1 += x
18+
total2 += x * enabled
19+
20+
print('Part 1:', total1)
21+
print('Part 2:', total2)

0 commit comments

Comments
 (0)