-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.py
46 lines (40 loc) · 1.37 KB
/
day12.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
39
40
41
42
43
44
45
46
import re
def execute(in_file, c_init=0):
registers = {'a':0, 'b':0, 'c':c_init, 'd':0}
# read file
f = open(in_file, 'r')
lines = f.readlines()
line_counter = 0
while(line_counter < len(lines)):
l = lines[line_counter].replace('\n', '')
m = re.search('cpy (\d+) ([abcd])', l)
n = re.search('cpy ([abcd]) ([abcd])', l)
if m is not None:
registers[m.group(2)] = int(m.group(1))
line_counter += 1
elif n is not None:
registers[n.group(2)] = registers[n.group(1)]
line_counter += 1
elif 'inc' in l:
registers[l[-1]] += 1
line_counter += 1
elif 'dec' in l:
registers[l[-1]] -= 1
line_counter += 1
else:
m = re.search('jnz (\S+) (\d+)', l)
if m is None:
m = re.search('jnz (\S+) (-\d+)', l)
r = m.group(1)
if (r in 'abcd' and not(registers[r] == 0)):
line_counter += int(m.group(2))
elif r.isnumeric() and not(int(r) == 0):
line_counter += int(m.group(2))
else:
line_counter += 1
print('Register a contains {}'.format(registers['a']))
if __name__ == "__main__":
print('Part 1:')
execute('day12_input.txt')
print('Part 2:')
execute('day12_input.txt', c_init=1)