Day 3 Done

This commit is contained in:
Jake Pullen
2024-12-03 16:34:07 +00:00
parent 582411d634
commit 6959815188
2 changed files with 74 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
testing = 0
if testing ==0:
with open(r'2024/03\input.txt', 'r') as file:
input = file.read()
else:
input = 'xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))'
# find every exact instance of mul() and multiply the two numbers together
import re
# Regular expression to find all instances of mul() with its arguments
pattern = r'mul\((\d+),(\d+)\)'
matches = re.findall(pattern, input)
numbers = [(int(x), int(y)) for x, y in matches]
print(numbers)
answer = 0
for x, y in numbers:
answer += x*y
print(answer)
+48
View File
@@ -0,0 +1,48 @@
testing = 0
if testing ==0:
with open(r'2024/03\input.txt', 'r') as file:
input = file.read()
else:
input = "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))"
# find every exact instance of mul() and multiply the two numbers together
import re
# Regular expression to find all instances of mul() with its arguments
pattern = r'mul\((\d+),(\d+)\)'
disable_pattern = r"don't\(\)"
enable_pattern = r"do\(\)"
enabled_list = []
disabled_list = []
is_active = True
# Split the input string by mul() to handle the state switching
parts = re.split(r'(mul\(\d+,\d+\))', input)
#print(parts)
for part in parts:
if re.search(disable_pattern, part):
is_active = False
elif re.search(enable_pattern, part):
is_active = True
elif re.match(pattern, part):
match = re.match(pattern, part)
numbers = (int(match.group(1)), int(match.group(2)))
if is_active:
enabled_list.append(numbers)
else:
disabled_list.append(numbers)
print("Enabled list:", enabled_list)
print("Disabled list:", disabled_list)
answer = 0
for x, y in enabled_list:
answer += x*y
print(answer)