Inline some vars
This commit is contained in:
parent
f92209c366
commit
cb4f3d26d0
@ -4,26 +4,19 @@ import re
|
|||||||
data = get_data(day=3, year=2024)
|
data = get_data(day=3, year=2024)
|
||||||
|
|
||||||
def part_one():
|
def part_one():
|
||||||
regex = r"mul\((\d+),(\d+)\)"
|
matches = re.findall(r"mul\((\d+),(\d+)\)", data, re.MULTILINE)
|
||||||
matches = re.findall(regex, data, re.MULTILINE)
|
|
||||||
|
|
||||||
total = 0
|
total = 0
|
||||||
for match in matches:
|
for match in matches:
|
||||||
total += int(match[0]) * int(match[1])
|
total += int(match[0]) * int(match[1])
|
||||||
print(total)
|
print(total)
|
||||||
|
|
||||||
def part_two():
|
def part_two():
|
||||||
mul_regex = r"mul\((\d+),(\d+)\)"
|
matches = re.finditer(r"mul\((\d+),(\d+)\)", data, re.MULTILINE)
|
||||||
do_str = "do()"
|
|
||||||
dont_str = "don't()"
|
|
||||||
|
|
||||||
matches = re.finditer(mul_regex, data, re.MULTILINE)
|
|
||||||
|
|
||||||
total = 0
|
total = 0
|
||||||
for match in matches:
|
for match in matches:
|
||||||
start = match.start()
|
start = match.start()
|
||||||
do_match = data.rfind(do_str, 0, start)
|
do_match = data.rfind("do()", 0, start)
|
||||||
dont_match = data.rfind(dont_str, 0, start)
|
dont_match = data.rfind("don't()", 0, start)
|
||||||
|
|
||||||
if do_match == dont_match == -1: # Neither
|
if do_match == dont_match == -1: # Neither
|
||||||
total += int(match.group(1)) * int(match.group(2))
|
total += int(match.group(1)) * int(match.group(2))
|
||||||
@ -33,7 +26,6 @@ def part_two():
|
|||||||
continue
|
continue
|
||||||
else: # Don't
|
else: # Don't
|
||||||
continue
|
continue
|
||||||
|
|
||||||
print(total)
|
print(total)
|
||||||
|
|
||||||
part_one()
|
part_one()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user