This commit is contained in:
2024-12-03 09:13:31 +01:00
parent ae0b3d9871
commit e3da406cb3
5 changed files with 65 additions and 0 deletions

32
03/03-02.py Normal file
View File

@@ -0,0 +1,32 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
def mul(m:str) -> int:
pattern = r"(\d+),(\d+)"
match = re.search(pattern, m)
return int(match.group(1)) * int(match.group(2))
def parse_mul(l:str) -> int:
result = 0
mult = True
pattern = r'mul\(\d+,\d+\)|do\(\)|don\'t\(\)'
matches = re.findall(pattern, l)
print(matches)
for match in matches:
if match == "don't()":
mult = False
elif match == "do()":
mult = True
elif mult and "mul(" in match:
result += mul(match)
return result
if __name__ == "__main__":
full_input = ""
input_file = open("input.txt", "r")
for line in input_file:
full_input += line
input_file.close()
print(f'Solution:{parse_mul(full_input)}')