diff --git a/2024/3/3_2.py b/2024/3/3_2.py new file mode 100644 index 0000000..b3ea919 --- /dev/null +++ b/2024/3/3_2.py @@ -0,0 +1,31 @@ +#!/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=}') diff --git a/2024/3/input_example2.txt b/2024/3/input_example2.txt new file mode 100644 index 0000000..30032cb --- /dev/null +++ b/2024/3/input_example2.txt @@ -0,0 +1 @@ +xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))