Solved 2024/04 P1 + P2

This commit is contained in:
2024-12-04 20:53:21 +01:00
parent 0d35287000
commit 68d3e12001
8 changed files with 229 additions and 30 deletions
+17
View File
@@ -0,0 +1,17 @@
# # Second attempt
# instructions = ""
# with open(input_f) as file:
# for line in file:
# instructions += line
# print(instructions)
# #(do\(\).*?mul\((\d+),{1}(\d+)\))
# (?<=do\(\))([^d]*(mul\(\d+,\d+\))[^d]*)+(?=don't\(\))
# (?<=do\(\))[^m]*(mul\((\d+),{1}(\d+)\)*)*[^d]*(?=don't\(\))
+2 -5
View File
@@ -43,9 +43,6 @@ if part == 2:
if do:
m = get_re(r"mul\((\d+),{1}(\d+)\)",match)
result += (int(m.group(1))*int(m.group(2)))
print(result)
print(result)
# Not really happy with my code. I would prefer to find all mul(x,y) between do() and don't(),
# instead of finding all the mul/do/don't and then turning calculation on/off.
#(do\(\).*?mul\((\d+),{1}(\d+)\))
# instead of finding all the mul/do/don't and then turning calculation on/off.
-16
View File
@@ -1,16 +0,0 @@
from re import findall
total1 = total2 = 0
enabled = True
data = open('input').read()
for a, b, do, dont in findall(r"mul\((\d+),(\d+)\)|(do\(\))|(don't\(\))", data):
if do or dont:
enabled = bool(do)
else:
x = int(a) * int(b)
total1 += x
total2 += x * enabled
print(total1, total2)