-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintcode.py
executable file
·136 lines (101 loc) · 2.71 KB
/
intcode.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
DEPTH_MAX = 5
PHASE_MIN = 0
PHASE_MAX = 4
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, input):
ints = ints.copy()
output = []
pos = 0
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])))
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:
break
else:
print("Unknown opcode \"{}\"".format(ints[pos]), file=sys.stderr)
exit(1)
return output
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(depth, script, input, history):
global DEPTH_MAX
global PHASE_MIN
global PHASE_MAX
global BEST_RESULT
global BEST_CONFIG
for phase in range(PHASE_MIN, PHASE_MAX+1):
if phase in history: continue
newhist = history.copy()
newhist.append(phase)
output = run_script(script, [phase,input])
output = output[0]
if depth < DEPTH_MAX:
run(depth+1, script, output, newhist)
else:
print("{} ---> {}".format(ints_to_str(BEST_CONFIG), output))
if output > BEST_RESULT:
BEST_RESULT = output
BEST_CONFIG = newhist
if __name__ == "__main__":
script = str_to_ints(sys.stdin.read())
run(1, script, 0, [])
print("!! {} ---> {}".format(ints_to_str(BEST_CONFIG), BEST_RESULT))