Compare commits

..

2 Commits

Author SHA1 Message Date
bb31161c80 No need for defs 2024-12-03 14:56:29 -04:00
cb4f3d26d0 Inline some vars 2024-12-03 14:54:54 -04:00

View File

@ -3,38 +3,26 @@ import re
data = get_data(day=3, year=2024) data = get_data(day=3, year=2024)
def part_one(): # 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
for match in matches:
total += int(match[0]) * int(match[1])
print(total)
total = 0 # Part two
for match in matches: matches = re.finditer(r"mul\((\d+),(\d+)\)", data, re.MULTILINE)
total += int(match[0]) * int(match[1]) total = 0
print(total) for match in matches:
start = match.start()
def part_two(): do_match = data.rfind("do()", 0, start)
mul_regex = r"mul\((\d+),(\d+)\)" dont_match = data.rfind("don't()", 0, start)
do_str = "do()" if do_match == dont_match == -1: # Neither
dont_str = "don't()" total += int(match.group(1)) * int(match.group(2))
continue
matches = re.finditer(mul_regex, data, re.MULTILINE) if do_match > dont_match: # Do
total += int(match.group(1)) * int(match.group(2))
total = 0 continue
for match in matches: else: # Don't
start = match.start() continue
do_match = data.rfind(do_str, 0, start) print(total)
dont_match = data.rfind(dont_str, 0, start)
if do_match == dont_match == -1: # Neither
total += int(match.group(1)) * int(match.group(2))
continue
if do_match > dont_match: # Do
total += int(match.group(1)) * int(match.group(2))
continue
else: # Don't
continue
print(total)
part_one()
part_two()