-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03.py
38 lines (27 loc) · 921 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
31
32
33
34
35
36
37
38
import re
use_example_input = False
def main():
program = read_input()
multi_sum = 0
enabled = True
while len(program) > 0:
piece, program = get_next_piece(program, enabled)
if enabled:
multi_sum += sum(x * y for x, y in find_mul_instructions(piece))
enabled = not enabled
print(multi_sum)
def get_next_piece(program, enabled):
switch_command = "don't()" if enabled else "do()"
index = program.find(switch_command)
piece = program[:index]
program = program[index:] if index >= 0 else ""
return piece, program
def find_mul_instructions(code):
matches = re.findall(r"mul\((\d+),(\d+)\)", code)
return [(int(x), int(y)) for x, y in matches]
def read_input():
file_name = "example03.txt" if use_example_input else "input03.txt"
with open(file_name, "r") as f:
return f.read()
if __name__ == "__main__":
main()