-
Notifications
You must be signed in to change notification settings - Fork 0
/
23.py
93 lines (86 loc) · 2.33 KB
/
23.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
print(chr(27)+'[2j')
print('\033c', end='')
f = open('23.test', 'r')
f = open('23.input', 'r')
lines = [x.strip() for x in f.readlines()]
def solve(registers):
idx = 0
count = 0
while idx < len(lines):
count += 1
if count % 1000000 == 0:
print(count, registers)
line = lines[idx]
print(line)
print(idx, registers)
parts = line.split()
if parts[0] == 'cpy':
if parts[1].isalpha():
registers[parts[2]] = registers[parts[1]]
else:
registers[parts[2]] = int(parts[1])
elif parts[0] == 'mul':
registers[parts[3]] = registers[parts[1]] * registers[parts[2]]
elif parts[0] == 'inc':
registers[parts[1]] = registers[parts[1]] + 1
elif parts[0] == 'dec':
registers[parts[1]] = registers[parts[1]] - 1
elif parts[0] == 'tgl':
tgl_idx = idx + registers[parts[1]]
if tgl_idx < len(lines):
tgl_line = lines[tgl_idx]
tgl_parts = tgl_line.split()
if len(tgl_parts) == 2:
if tgl_parts[0] == 'inc':
tgl_parts[0] = 'dec'
else:
tgl_parts[0] = 'inc'
else:
if tgl_parts[0] == 'jnz':
tgl_parts[0] = 'cpy'
else:
tgl_parts[0] = 'jnz'
lines[tgl_idx] = ' '.join(tgl_parts)
elif parts[0] == 'jnz':
if parts[1].isalpha():
if registers[parts[1]] != 0:
idx += int(parts[2])
continue
else:
if parts[1] != 0:
if parts[2].isalpha():
idx += registers[parts[2]]
else:
idx += int(parts[2])
continue
idx += 1
return registers['a']
result = solve({'a': 12, 'b': 0, 'c': 0, 'd': 0})
print("Solution part 1:", result)
# Part 2 - Updated and optimized code
# cpy a b
# dec b
# cpy a d
# cpy 0 a
# cpy b c
# mul d c a
# cpy 0 c
# cpy 0 c
# cpy 0 d
# cpy 0 d
# dec b
# cpy b c
# cpy c d
# dec d
# inc c
# jnz d -2
# tgl c
# cpy -16 c
# jnz 1 c
# cpy 87 c
# jnz 97 d
# inc a
# inc d
# jnz d -2
# inc c
# jnz c -5