-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintcode.py
67 lines (60 loc) · 2.11 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
class Instruction:
def __init__(self, pos, source):
self.opcode = int(source[pos])
if self.opcode != 99:
self.input1 = int(source[pos+1])
self.input2 = int(source[pos+2])
self.output_pos = int(source[pos+3])
def process(self, source):
exit = 0
if self.opcode == 1:
source[self.output_pos] = int(source[self.input1])+int(source[self.input2])
elif self.opcode == 2:
source[self.output_pos] = int(source[self.input1])*int(source[self.input2])
elif self.opcode == 99:
exit = 1
else:
print "Invalid opcode: ({})".format(self.opcode)
return exit
def debug_print(self, pos, source):
if self.opcode == 99:
print "End of program"
print "Data segment:"
pos = pos + 1
while pos < len(source):
print "{}".format(source[pos])
pos = pos + 1
else:
print "opcode: {} input1: {} input2: {} output_pos: {}".format(self.opcode, self.input1, self.input2, self.output_pos)
def run_program(source, noun, verb):
instructions = list(source)
instructions[1] = noun
instructions[2] = verb
exit = 0
pos = 0
while exit == 0:
i = Instruction(pos, instructions)
exit = i.process(instructions)
pos = pos + 4
result = int(instructions[0])
print "noun={} verb={} result={}".format(noun, verb, result)
return result
# main method
with open('input-day2') as fp:
line = fp.readline()
starting_instructions = line.split(',')
print "Testing..."
result = run_program(starting_instructions, 12, 2)
if result == 4090701:
print "Processing..."
noun = 0
for noun in range(100):
verb = 0
for verb in range(100):
result = run_program(starting_instructions, noun, verb)
if result == 19690720:
target_noun = noun
target_verb = verb
print "target_noun = {} target_verb = {}".format(target_noun, target_verb)
print "Finished"
fp.close()