02
This commit is contained in:
32
03/03-02.py
Normal file
32
03/03-02.py
Normal 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)}')
|
||||
Reference in New Issue
Block a user