Solve 2024 day 3
This commit is contained in:
parent
893a8e3dbb
commit
b627e97c5a
26
2024/d3.py
Normal file
26
2024/d3.py
Normal file
@ -0,0 +1,26 @@
|
||||
import re
|
||||
from lib import get_data
|
||||
from lib import ints
|
||||
|
||||
|
||||
data = get_data(__file__)
|
||||
|
||||
t = 0
|
||||
r = re.compile(r'mul\(\d+,\d+\)')
|
||||
for match in r.findall(data):
|
||||
a, b = ints(match)
|
||||
t += a * b
|
||||
print(t)
|
||||
|
||||
t = 0
|
||||
enabled = True
|
||||
for i in range(len(data.strip())):
|
||||
m = r.match(data[i:])
|
||||
if m and enabled:
|
||||
a, b = ints(m.group())
|
||||
t += a * b
|
||||
elif data[i:].startswith("do()"):
|
||||
enabled = True
|
||||
elif data[i:].startswith("don't()"):
|
||||
enabled = False
|
||||
print(t)
|
Loading…
Reference in New Issue
Block a user