advent_of_code/2024/3/3_2.py

32 lines
807 B
Python
Raw Permalink Normal View History

2024-12-04 18:16:59 +01:00
#!/usr/bin/env python
# 2024 - Advent Of Code 3 - part 2
import re
# file = 'input_example2.txt'
file = 'input.txt'
# pylint: disable=consider-using-with
input_lines = [line.strip('\n') for line in open(file, encoding="utf-8")]
multilines = ''.join(input_lines)
re_mul = re.compile(r'mul\((\d+),(\d+)\)')
fmultilines = re.sub(r'don\'t\(\).+?do\(\)', '', multilines)
# dirty hack, use 3 times in a row to remove all trailling don't()
fmultilines2 = re.sub(r'(.+)don\'t\(\).*', r'\1', fmultilines)
fmultilines3 = re.sub(r'(.+)don\'t\(\).*', r'\1', fmultilines2)
fmultilines4 = re.sub(r'(.+)don\'t\(\).*', r'\1', fmultilines3)
accum = 0
m = re_mul.findall(fmultilines4)
if m:
print(m)
m_list = [int(x[0]) * int(x[1]) for x in m]
# print(m_list)
accum += sum(m_list)
print(f'{accum=}')