-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart2.py
More file actions
78 lines (71 loc) · 1.51 KB
/
Copy pathpart2.py
File metadata and controls
78 lines (71 loc) · 1.51 KB
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
from collections import *
from functools import *
from itertools import *
from math import *
import re
def ints(s):
return list(map(int, re.findall(r'-?\d+', s)))
# Initialize the values based on the given input
A = 64012472
B = 0
C = 0
program = [2, 4, 1, 7, 7, 5, 0, 3, 1, 7, 4, 1, 5, 5, 3, 0]
ip = 0
def combo(x):
if x == 0: return 0
if x == 1: return 1
if x == 2: return 2
if x == 3: return 3
if x == 4: return A
if x == 5: return B
if x == 6: return C
return None
def step():
global ip, A, B, C
instr = program[ip]
opcode = program[ip + 1]
ip += 2
if instr == 0:
A = A // (2 ** combo(opcode))
if instr == 1:
B = B ^ opcode
if instr == 2:
B = combo(opcode) % 8
if instr == 3:
if A != 0:
ip = opcode
if instr == 4:
B = B ^ C
if instr == 5:
return combo(opcode) % 8
if instr == 6:
B = A // (2 ** combo(opcode))
if instr == 7:
C = A // (2 ** combo(opcode))
def run(a):
global A, B, C, ip
ip = 0
A = a
B = 0
C = 0
result = []
while True:
try:
x = step()
except Exception:
break
if x is not None:
result.append(x)
return result
# Program: 2,4,1,7,7,5,0,3,1,7,4,1,5,5,3,0
a = 35282534841844
prev = 0
while True:
x = run(a)
if x[:6] == [2, 4, 1, 2, 7, 5]:
print(a, len(x), a - prev, x)
prev = a
if x == program:
print(a)
break
a += 2097152