Spaces:
Running
Running
File size: 884 Bytes
a4da721 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import re
file = "./input.txt"
def solve_part1(input_string):
pattern = r"mul\((\d{1,3}),(\d{1,3})\)"
matches = re.findall(pattern, input_string)
total = 0
for match in matches:
total += int(match[0]) * int(match[1])
return str(total)
def solve_part2(input_string):
pattern = r"mul\((\d{1,3}),(\d{1,3})\)|do\(\)|don't\(\)"
matches = re.findall(pattern, input_string)
total = 0
enabled = True
for match in matches:
if match[0] and match[1]:
if enabled:
total += int(match[0]) * int(match[1])
elif match[0:2] == "do":
enabled = True
elif match[0:5] == "don't":
enabled = False
return str(total)
with open(file, 'r') as f:
input_string = f.read()
result1 = solve_part1(input_string)
print(result1)
result2 = solve_part2(input_string)
print(result2) |