-
Notifications
You must be signed in to change notification settings - Fork 0
/
day5.py
116 lines (85 loc) · 2.29 KB
/
day5.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
# day2.py
import math
# f = open("./fileDatas/day2.txt", "r")
def calculateOutput(unit):
f = open("./inputs/day5.txt", "r")
fileInput = f.read()
fileInput = fileInput.split(',')
fileData = []
testOutputs = []
# Create an array of integers, there's probably a better way...
for i in range(len(fileInput)):
fileData.append(int(fileInput[i]))
print(len(fileData), "elements in", fileData)
pos = 0
while True:
code = int(fileData[pos])
orgCode = code
opcode = int(code%100)
modes = [0,0]
params = []
code -= opcode
code =int(code/100)
i = 0
while code != 0:
modes[i] = code%10
code = int(code/10)
i += 1
print("Starting at pos", pos, "with", orgCode, "and opcode", opcode)
if opcode == 99:
print("Reached end, last output")
break
if opcode == 1 or opcode == 2:
if modes[0] == 0:
params.append(fileData[fileData[pos+1]])
else:
params.append(fileData[pos+1])
if modes[1] == 0:
params.append(fileData[fileData[pos+2]])
else:
params.append(fileData[pos+2])
if opcode == 1:
result = params[0] + params[1]
print(params[0], "+", params[1], "=", result, "stored at", fileData[pos+3])
else:
result = params[0] * params[1]
print(params[0], "*", params[1], "=", result, "stored at", fileData[pos+3])
fileData[fileData[pos+3]] = result
pos += 4
elif opcode == 3:
position = fileData[pos + 1]
print("Old value", fileData[position])
fileData[position] = unit
print(unit, "stored at position", position)
print("New value", fileData[position])
pos += 2
elif opcode == 4:
position = fileData[pos+1]
print("Added output", fileData[position])
testOutputs.append(fileData[position])
pos += 2
while False:
opcode = int(fileData[pos])
if opcode == 99:
break
else:
input1 = int(fileData[int(fileData[pos+1])])
#print("input1 is", input1)
input2 = int(fileData[int(fileData[pos+2])])
#print("input2 is", input2)
newPos = int(fileData[pos+3])
if opcode == 1:
result = input1 + input2
elif opcode == 2:
result = input1*input2
elif opcode == 3:
print("3")
elif opcode == 4:
print("4")
fileData[newPos] = result
pos += 4
f.close()
return testOutputs
#calculateOutput(12,2)
#calculateOutput(12,4)
print(calculateOutput(1))