From 262c8c0814ccc9299e4e4fc7656ae4d3537585bb Mon Sep 17 00:00:00 2001 From: Armin Friedl Date: Tue, 3 Dec 2024 07:06:30 +0100 Subject: [PATCH] Day03 --- day03.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 day03.py diff --git a/day03.py b/day03.py new file mode 100644 index 0000000..9b55a34 --- /dev/null +++ b/day03.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +import re + +with open("day03") as f: + i = f.read() + +# 1 + +pat = re.compile(r"mul\(([0-9]+),([0-9]+)\)") + +muls = re.findall(pat, i) + +res = 0 +for mul in muls: + res += int(mul[0]) * int(mul[1]) + + +# 2 + +pat = re.compile(r"(mul\(([0-9]+),([0-9]+)\)|do\(\)|don't\(\))") + +muls = re.findall(pat, i) + +res = 0 + +def consume_disabled(muls): + for mul in muls: + if mul[0] == "do()": + consume_enabled(muls) + +def consume_enabled(muls): + global res + for mul in muls: + if mul[0] == "don't()": + consume_disabled(muls) + if mul[0].startswith("mul"): + res += int(mul[1]) * int(mul[2]) + +gen = (m for m in muls) +consume_enabled(gen) + +print(res)