2024 - Day 3 part 2
This commit is contained in:
parent
daccd38f1f
commit
43415f0ffc
2 changed files with 32 additions and 0 deletions
31
2024/3/3_2.py
Normal file
31
2024/3/3_2.py
Normal file
|
@ -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=}')
|
1
2024/3/input_example2.txt
Normal file
1
2024/3/input_example2.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
|
Loading…
Reference in a new issue