-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_05a.py
60 lines (51 loc) · 1.63 KB
/
day_05a.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
#!/usr/bin/python3
import sys
def getNOps(n, inst_ptr, modes, program):
ops = list()
for i in range(0, n):
rem = modes % 10
modes = int(modes / 10)
if rem == 0:
ops.append(program[program[inst_ptr + i + 1]])
else:
ops.append(program[inst_ptr + i + 1])
return ops
def main():
# Get input
file_name = "../input/day_05_input"
input_position = 1
if len(sys.argv) > 1 and not sys.argv[1].isnumeric():
file_name = sys.argv[1]
input_position += 1
file = open(file_name)
program = [int(ele) for ele in file.readline().strip("\n").split(",")]
# Solve
inst_ptr = 0
output = -1
while program[inst_ptr] != 99:
instr = program[inst_ptr]
opcode = instr % 100
modes = int(instr / 100)
if opcode == 1:
ops = getNOps(2, inst_ptr, modes, program)
program[program[inst_ptr + 3]] = ops[0] + ops[1]
inst_ptr += 4
elif opcode == 2:
ops = getNOps(2, inst_ptr, modes, program)
program[program[inst_ptr + 3]] = ops[0] * ops[1]
inst_ptr += 4
elif opcode == 3:
program[program[inst_ptr + 1]] = int(
sys.argv[input_position]
) # Based on input
# print("Input: {}".format(int(sys.argv[input_position])))
inst_ptr += 2
input_position += 1
elif opcode == 4:
# print(program[program[inst_ptr + 1]])
output = program[program[inst_ptr + 1]]
inst_ptr += 2
print(output)
return output
if __name__ == "__main__":
main()