Day03
This commit is contained in:
parent
4396296807
commit
262c8c0814
1 changed files with 43 additions and 0 deletions
43
day03.py
Normal file
43
day03.py
Normal file
|
@ -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)
|
Loading…
Reference in a new issue