Skip to content

Commit ef3fa3c

Browse files
committed
Day 3! This one was irritating because I was using the wrong test input on b for ages
1 parent 534bf34 commit ef3fa3c

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

03/03a.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import re
2+
3+
with open('input', 'r') as f: instructions = f.read()
4+
5+
results = re.findall(r"mul\([0-9]{1,3},[0-9]{1,3}\)", instructions)
6+
7+
total = 0
8+
for result in results:
9+
a, b = result.replace("mul(","").replace(")","").split(",")
10+
print(f"{result=} {a=} {b=} {int(a) * int(b)=}")
11+
total += int(a) * int(b)
12+
13+
print(total)

03/03b.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import re
2+
3+
with open('input', 'r') as f: instructions = f.read()
4+
5+
total = 0
6+
7+
do = True
8+
print(instructions)
9+
while instructions:
10+
#print(total)
11+
if do:
12+
# First check there's not a dont() coming up
13+
print(instructions[:7])
14+
if instructions[:7] == "don't()":
15+
do = False
16+
continue
17+
# Then check if there's a valid mul(xxx,yyy) coming up
18+
else:
19+
match = re.search(r"mul\([0-9]{1,3},[0-9]{1,3}\)", instructions[:12])
20+
if match:
21+
#print(match.group())
22+
a, b = match.group().replace("mul(","").replace(")","").split(",")
23+
total += int(a) * int(b)
24+
#print(match.span())
25+
instructions = instructions[match.span()[1]:]
26+
continue
27+
else:
28+
instructions = instructions[1:]
29+
continue
30+
31+
else:
32+
# Check if there's a do() coming up
33+
print(instructions[:4])
34+
if instructions[:4] == "do()":
35+
do = True
36+
continue
37+
else:
38+
instructions = instructions[1:]
39+
40+
print(total)

0 commit comments

Comments
 (0)