-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03.py
30 lines (23 loc) · 799 Bytes
/
day03.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import re
with open("input.txt") as input_file:
input_lines = input_file.readlines()
input_lines = [line.strip('\n') for line in input_lines]
answer_1 = 0
for line in input_lines:
muls = re.findall(r"mul\((\d+),(\d+)\)", line)
for mul in muls:
answer_1 += int(mul[0]) * int(mul[1])
print("Answer 1:", answer_1) # 167090022
answer_2 = 0
enabled = True # (note: this is set just once, not once per line!)
for line in input_lines:
cmds = re.findall(r"mul\((\d+),(\d+)\)|(do)\(\)|(don)'t\(\)", line)
for cmd in cmds:
if cmd[2] == "do":
enabled = True
elif cmd[3] == "don":
enabled = False
else:
if enabled:
answer_2 += int(cmd[0]) * int(cmd[1])
print("Answer 2:", answer_2) # 89823704