44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
import sys
|
|
import os
|
|
import re
|
|
sys.path.insert(1, os.path.abspath('../../'))
|
|
from python_tools.aoc_utils import *
|
|
|
|
def part_one(input):
|
|
pt_1=0
|
|
list_of_mults = re.findall(r"mul\(\d{1,3},\d{1,3}\)", input)
|
|
for i in list_of_mults:
|
|
numbers = re.findall(r"\d{1,3}", i)
|
|
pt_1 += int(numbers[0]) * int(numbers[1])
|
|
# print(numbers)
|
|
print("Part One: ", pt_1)
|
|
|
|
def part_two(input):
|
|
off=False
|
|
pt_2=0
|
|
re1 = r"mul\(\d{1,3},\d{1,3}\)"
|
|
re2 = r"do\(\)"
|
|
re3 = r"don't\(\)"
|
|
list_of_mults = re.compile("(%s|%s|%s)" % (re1, re2, re3)).findall(input)
|
|
# list_of_mults = re.findall(r"do\(\)"+r"mul\(\d{1,3},\d{1,3}\)", input)
|
|
for i in list_of_mults:
|
|
if "don't()" == i:
|
|
off = True
|
|
elif "do()" == i:
|
|
off = False
|
|
else:
|
|
if not off:
|
|
numbers = re.findall(r"\d{1,3}", i)
|
|
pt_2 += int(numbers[0]) * int(numbers[1])
|
|
print("Part One: ", pt_2)
|
|
|
|
def main():
|
|
f = open("input.txt", 'r')
|
|
input = f.read()
|
|
part_one(input)
|
|
part_two(input)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|