This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_2_1.py
74 lines (53 loc) · 2.87 KB
/
day_2_1.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
#!/usr/bin/python3 -u
import sys
def puzzle(filename):
idx_opcode = 0
with open(filename, "r") as f:
while True:
line = f.readline()
if not line:
break
if len(line) > 0:
intcode_str = line.split(",")
intcode = [int(x) for x in intcode_str]
intcode[1] = 12
intcode[2] = 2
while idx_opcode < len(intcode):
opcode = intcode[idx_opcode]
# print("INFO: Found opcode %d at %d, Operation Set: [%d, %d, %d, %d]" % (opcode, idx_opcode,
# intcode[idx_opcode],
# intcode[idx_opcode + 1],
# intcode[idx_opcode + 2],
# intcode[idx_opcode + 3]))
if opcode == 99:
# print("INFO: Opcode 99 found at %d" % idx_opcode)
break
elif opcode == 1:
try:
# print("INFO: Getting op1 ([%d] = %d) and op2 ([%d] = %d) for opcode 1" %
# (intcode[idx_opcode + 1], intcode[intcode[idx_opcode + 1]], intcode[idx_opcode + 2],
# intcode[intcode[idx_opcode + 2]]))
val = intcode[intcode[idx_opcode + 1]] + \
intcode[intcode[idx_opcode + 2]]
except IndexError as e:
print("ERROR: %s" % e, file=sys.stderr)
else:
intcode[intcode[idx_opcode + 3]] = val
elif opcode == 2:
try:
# print("INFO: Getting op1 ([%d] = %d) and op2 ([%d] = %d) for opcode 2" %
# (intcode[idx_opcode + 1], intcode[intcode[idx_opcode + 1]], intcode[idx_opcode + 2],
# intcode[intcode[idx_opcode + 2]]))
val = intcode[intcode[idx_opcode + 1]] * intcode[intcode[idx_opcode + 2]]
except IndexError as e:
print("ERROR: %s" % e, file=sys.stderr)
else:
intcode[intcode[idx_opcode + 3]] = val
else:
print("ERROR: Unknown Opcode %d found" % opcode)
idx_opcode += 4
print("\n\nINFO: Program Halted, with [0] = %d" % intcode[0])
def main():
puzzle("input_day_2_1.txt")
if __name__ == "__main__":
main()