36 lines
776 B
Python
36 lines
776 B
Python
import re
|
|
|
|
with open('input.text') as file:
|
|
data: str = file.read()
|
|
|
|
# Part 1
|
|
|
|
instructions = re.findall("mul\([0-9]+,[0-9]+\)", data)
|
|
|
|
total = 0
|
|
for instruction in instructions:
|
|
nums = [int(n) for n in re.findall("[0-9]+", instruction)]
|
|
total += nums[0] * nums[1]
|
|
|
|
print(f'Part 1: {total}')
|
|
|
|
# Part 2
|
|
|
|
instructions = re.findall("mul\([0-9]+,[0-9]+\)|don't\(\)|do\(\)", data)
|
|
|
|
total = 0
|
|
enabled = True
|
|
for instruction in instructions:
|
|
match instruction:
|
|
case "don't()":
|
|
enabled = False
|
|
case "do()":
|
|
enabled = True
|
|
case _:
|
|
if not enabled:
|
|
continue
|
|
nums = [int(n) for n in re.findall("[0-9]+", instruction)]
|
|
total += nums[0] * nums[1]
|
|
|
|
print(f'Part 2: {total}')
|