Solved 2024/04 P1 + P2
This commit is contained in:
@@ -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
@@ -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.
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user