Spaces:
Running
Running
| import re | |
| file = "./input.txt" | |
| def solve_part1(input_string): | |
| pattern = r"mul\((\d{1,3}),(\d{1,3})\)" | |
| matches = re.findall(pattern, input_string) | |
| total = 0 | |
| for match in matches: | |
| total += int(match[0]) * int(match[1]) | |
| return str(total) | |
| def solve_part2(input_string): | |
| pattern = r"mul\((\d{1,3}),(\d{1,3})\)|do\(\)|don't\(\)" | |
| matches = re.findall(pattern, input_string) | |
| total = 0 | |
| enabled = True | |
| for match in matches: | |
| if match[0] and match[1]: | |
| if enabled: | |
| total += int(match[0]) * int(match[1]) | |
| elif match[0:2] == "do": | |
| enabled = True | |
| elif match[0:5] == "don't": | |
| enabled = False | |
| return str(total) | |
| with open(file, 'r') as f: | |
| input_string = f.read() | |
| result1 = solve_part1(input_string) | |
| print(result1) | |
| result2 = solve_part2(input_string) | |
| print(result2) |