-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintcode-feedback.py
executable file
·191 lines (138 loc) · 3.62 KB
/
intcode-feedback.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
DEPTH_MAX = 5
PHASE_MIN = 5
PHASE_MAX = 9
BEST_RESULT = 0
BEST_CONFIG = []
def decode_opcode(value):
op = value % 100
value = value // 100
argmode = []
for i in range(3):
argmode.append(value % 10)
value = value // 10
return op, argmode
def get_val(ints, position, mode):
if mode == 0:
return ints[position]
elif mode == 1:
return position
else:
print("What the fuck", file=sys.stderr)
exit(1)
def run_script(ints, start_pos, input):
output = []
finished = False
pos = start_pos
while True:
op, argmode = decode_opcode(ints[pos])
if op == 1:
arg1 = ints[pos+1]
arg2 = ints[pos+2]
arg3 = ints[pos+3]
pos += 4
ints[arg3] = get_val(ints, arg1, argmode[0]) + get_val(ints, arg2, argmode[1])
elif op == 2:
arg1 = ints[pos+1]
arg2 = ints[pos+2]
arg3 = ints[pos+3]
pos += 4
ints[arg3] = get_val(ints, arg1, argmode[0]) * get_val(ints, arg2, argmode[1])
elif op == 3:
arg = ints[pos+1]
pos += 2
ints[arg] = input.pop(0)
elif op == 4:
arg = ints[pos+1]
pos += 2
output.append(get_val(ints, arg, argmode[0]))
# print("Opcode 4: {}".format(get_val(ints, arg, argmode[0])))
break
elif (op == 5) or (op == 6):
arg1 = ints[pos+1]
arg2 = ints[pos+2]
val = get_val(ints, arg1, argmode[0])
if ((op == 5) and (val != 0)) or ((op == 6) and (val == 0)):
pos = get_val(ints, arg2, argmode[1])
else:
pos += 3
elif (op == 7) or (op == 8):
arg1 = ints[pos+1]
arg2 = ints[pos+2]
arg3 = ints[pos+3]
pos += 4
val1 = get_val(ints, arg1, argmode[0])
val2 = get_val(ints, arg2, argmode[1])
condition = ((op == 7) and (val1 < val2)) or ((op == 8) and (val1 == val2))
ints[arg3] = 1 if condition else 0
elif op == 99:
finished = True
break
else:
print("Unknown opcode \"{}\"".format(ints[pos]), file=sys.stderr)
exit(1)
return pos, output, finished
def str_to_ints(string):
return list(map(lambda x: int(x), string.split(',')))
def ints_to_str(ints):
return ",".join(list(map(lambda x: str(x), ints)))
def run(config, script):
global DEPTH_MAX, BEST_RESULT, BEST_CONFIG
scripts = []
script_pos = []
for i in range(DEPTH_MAX):
scripts.append(script.copy())
script_pos.append(0)
input = 0
output = None
idx = 0
firstloop = True
repeat = True
while True:
if firstloop:
input = [config[idx], input]
else:
input = [input]
# print("running script #{} from [{}] -- input: [{}]".format(idx, ints_to_str(config), ints_to_str(input)))
endpos, output, finished = run_script(scripts[idx], script_pos[idx], input)
script_pos[idx] = endpos
# Short-circuit
if len(output) == 0:
output = input[0]
break
output = output[0]
if finished: repeat = False
idx += 1
input = output
if idx == DEPTH_MAX:
if repeat:
idx = 0
firstloop = False
else:
break
print("{} ---> {}".format(ints_to_str(config), output))
if output > BEST_RESULT:
BEST_RESULT = output
BEST_CONFIG = config
def generate_config_r(conf, depth, max_depth):
global PHASE_MIN, PHASE_MAX
result = []
for phase in range(PHASE_MIN, PHASE_MAX+1):
if phase in conf: continue
entry = conf.copy()
entry.append(phase)
if depth == max_depth:
result.append(entry)
else:
result += generate_config_r(entry, depth + 1, max_depth)
return result
def generate_configs():
return generate_config_r([], 0, DEPTH_MAX-1)
if __name__ == "__main__":
script = str_to_ints(sys.stdin.read())
configs = generate_configs()
for i in range(len(configs)):
run(configs[i], script)
print("!! {} ---> {}".format(ints_to_str(BEST_CONFIG), BEST_RESULT))