2024 Day 3
This commit is contained in:
Executable
+14
@@ -0,0 +1,14 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
import re
|
||||||
|
from math import prod
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
with open('input.txt', 'r' ) as input_file:
|
||||||
|
input = input_file.read()
|
||||||
|
|
||||||
|
res = 0
|
||||||
|
for cmd in re.findall(r"mul\(\d+,\d+\)", input, flags=re.MULTILINE):
|
||||||
|
digits = [int(y) for y in ''.join([x for x in cmd if x.isdigit() or x == ',']).split(',')]
|
||||||
|
res += prod(digits)
|
||||||
|
print(res)
|
||||||
Executable
+22
@@ -0,0 +1,22 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
import re
|
||||||
|
from math import prod
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
with open('input.txt', 'r' ) as input_file:
|
||||||
|
input = input_file.read()
|
||||||
|
|
||||||
|
res = 0
|
||||||
|
en = True
|
||||||
|
for cmd in re.findall(r"(do\(\)|don\'t\(\)|mul\(\d+,\d+\))", input, flags=re.MULTILINE):
|
||||||
|
if cmd == 'do()':
|
||||||
|
en = True
|
||||||
|
continue
|
||||||
|
if cmd =='don\'t()':
|
||||||
|
en = False
|
||||||
|
continue
|
||||||
|
if en:
|
||||||
|
digits = [int(y) for y in ''.join([x for x in cmd if x.isdigit() or x == ',']).split(',')]
|
||||||
|
res += prod(digits)
|
||||||
|
print(res)
|
||||||
Reference in New Issue
Block a user