-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-2.py
87 lines (76 loc) · 2.19 KB
/
2-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
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
import csv
# Read file input
with open("2.txt", "r") as f:
reader = csv.reader(f)
codes = list(reader)[0]
codes = list(map(int, codes))
print("codes: ", codes)
# Define function for multiple tests
def test_inputs(noun, verb, codes):
codes[1] = noun
codes[2] = verb
for i, code in enumerate(codes):
# print("index: ", i)
# advance 4 entries
if i != 0 and (i) % 4 != 0:
continue
if code == 1:
# print("opcode: 1")
pos1 = codes[i + 1]
pos2 = codes[i + 2]
pos3 = codes[i + 3]
val1 = codes[pos1]
val2 = codes[pos2]
# print("pos1: ", pos1)
# print("pos2: ", pos2)
# print("pos3: ", pos3)
# print("val1: ", val1)
# print("val2: ", val2)
# print("result: ", val1 + val2)
codes[pos3] = val1 + val2
elif code == 2:
# print("opcode: 2")
pos1 = codes[i + 1]
pos2 = codes[i + 2]
pos3 = codes[i + 3]
val1 = codes[pos1]
val2 = codes[pos2]
# print("pos1: ", pos1)
# print("pos2: ", pos2)
# print("pos3: ", pos3)
# print("val1: ", val1)
# print("val2: ", val2)
# print("result: ", val1 * val2)
codes[pos3] = val1 * val2
elif code == 99:
# print("opcode: 99")
break
else:
# print("unknown opcode")
break
if codes[0] == 19690720:
print("codes: ", codes)
return True
else:
return False
# print("")
# print("codes: ", codes)
# print("")
# print("i: ", i)
# print("code: ", code)
# Prepare inputs
noun = list(range(0, 100))
verb = list(range(0, 100))
# Loop through all possibilities
for n in noun:
outer = False # break outer loop when solution found
for v in verb:
c = codes.copy()
if test_inputs(n, v, c):
print("noun: ", n)
print("verb: ", v)
print("final answer (100 * noun + verb): ", 100 * n + v)
outer = True
break
if outer:
break