advent-of-code/2024/python/day03.py
2024-12-03 21:24:00 -05:00

52 lines
1.1 KiB
Python

import shared
import re
from scanf import scanf
# @shared.profile
def part1(rows):
total = 0
r = re.compile(r'mul\(\d+,\d+\)')
for row in rows:
muls = r.findall(row)
for m in muls:
x, y = scanf("mul(%d,%d)", m)
total += x * y
print(total)
# @shared.profile
def part2(rows):
total = 0
r = re.compile(r'(mul\(\d+,\d+\)|do\(\)|don\'t\(\))')
enabled = True
for row in rows:
muls = r.findall(row)
for m in muls:
if m == "don't()":
enabled = False
continue
if m == "do()":
enabled = True
continue
if enabled:
x, y = scanf("mul(%d,%d)", m)
total += x * y
print(total)
def main():
rows = [row for row in shared.load_rows(3)]
with shared.elapsed_timer() as elapsed:
part1(rows)
print("🕒", elapsed())
rows = [row for row in shared.load_rows(3, True)]
with shared.elapsed_timer() as elapsed:
part2(rows)
print("🕒", elapsed())
if __name__ == "__main__":
main()