-
Notifications
You must be signed in to change notification settings - Fork 0
/
q23_2.py
61 lines (46 loc) · 1.47 KB
/
q23_2.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
# Notice that instructions 9-23 just repeats themselves because of two loops
# One at index 19 and another at index 23
# So the answer of that loop can simply be generated noticing the patern in the loop
registers = {}
for register in range(ord('a'), ord('h')+1):
registers[chr(register)] = 0
def get_value(register):
value = 0
try:
value = int(register)
except:
registers.setdefault(register, 0)
value = registers[register]
return value
instructions = []
with open("in.txt", "r") as in_file:
for line in in_file:
instructions.append(line.strip().split())
def is_factorizable(b):
for i in range(2, int(pow(b, 0.5) + 2)):
if b%i == 0:
return True
return False
index = 0
registers['a'] = 1
while index < len(instructions):
instruction = instructions[index]
# Pattern in the loop
if index == 10:
if is_factorizable(registers['b']):
registers['f'] = 0
index = 24
continue
if instruction[0] == "set":
registers[instruction[1]] = get_value(instruction[2])
elif instruction[0] == "sub":
registers[instruction[1]] -= get_value(instruction[2])
elif instruction[0] == "mul":
registers[instruction[1]] *= get_value(instruction[2])
elif instruction[0] == "jnz":
check = get_value(instruction[1])
if check != 0:
index += get_value(instruction[2])
continue
index += 1
print registers['h']