did day3
This commit is contained in:
1 parent
8b27923932
commit
92131a8449
4 files changed
+43
No files matched your search
@@ -0,0 +1,35 @@
|
||||
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}')
|
||||
Reference in new issue
Block a user