-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.py
72 lines (55 loc) · 1.62 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import get_input
day = int(os.path.basename(__file__).replace('day', '').replace('.py', ''))
data = get_input.get_input(day)
lines = data.split('\n')
at = 0
reg = {'a': 0, 'b': 0, 'c': 0, 'd': 0}
while at < len(lines):
parts = lines[at].split(' ')
if parts[0] == 'inc':
reg[parts[1]] += 1
elif parts[0] == 'dec':
reg[parts[1]] -= 1
elif parts[0] == 'cpy':
if parts[1].lstrip('-').isdigit():
reg[parts[2]] = int(parts[1])
else:
reg[parts[2]] = reg[parts[1]]
elif parts[0] == 'jnz':
if parts[1].lstrip('-').isdigit():
check = int(parts[1])
else:
check = reg[parts[1]]
if check != 0:
at += int(parts[2])
continue
else:
print('Error at line {}'.format(lines[at]))
at += 1
print('Value in register a: {}'.format(reg['a']))
at = 0
reg = {'a': 0, 'b': 0, 'c': 1, 'd': 0}
while at < len(lines):
parts = lines[at].split(' ')
if parts[0] == 'inc':
reg[parts[1]] += 1
elif parts[0] == 'dec':
reg[parts[1]] -= 1
elif parts[0] == 'cpy':
if parts[1].lstrip('-').isdigit():
reg[parts[2]] = int(parts[1])
else:
reg[parts[2]] = reg[parts[1]]
elif parts[0] == 'jnz':
if parts[1].lstrip('-').isdigit():
check = int(parts[1])
else:
check = reg[parts[1]]
if check != 0:
at += int(parts[2])
continue
else:
print('Error at line {}'.format(lines[at]))
at += 1
print('Value in register a when c starts at 1: {}'.format(reg['a']))