Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 99 : Termination, 0 parameter
- # 1 : Addition, 3 parameter
- # 2 : Multiplication, 3 parameter
- # 3 : Input, 1 parameter
- # 4 : Output, 1 parameter
- # 5 : Jump-if-true, 2 parameter
- # 6 : Jump-if-false, 2 parameter
- # 7 : less-than, 3 parameter
- # 8 : equals, 3 parameter
- with open("AoC Day 5.txt", "r") as f:
- contents = [int(c) for c in f.read().split(',')]
- f.close()
- def intcode(vals):
- contents = vals.copy()
- x = 0
- while contents[x] != 99:
- instruction = str(contents[x])
- opcode = '0'*(5-len(str(contents[x]))) + str(contents[x])
- A, B, C, DE = int(opcode[0]), int(opcode[1]), int(opcode[2]), opcode[-2:]
- if DE in ('01', '02', '07', '08'):
- A = contents[x+3]#contents[contents[x+3]] if A==0 else contents[x+3]
- B = contents[contents[x+2]] if B==0 else contents[x+2]
- if DE in ('05', '06'):
- B = contents[contents[x+2]] if B==0 else contents[x+2]
- C = contents[contents[x+1]] if C==0 else contents[x+1]
- if DE in '01':
- contents[A] = B + C
- step_forward = 4
- if DE in '02':
- contents[A] = B * C
- step_forward = 4
- if DE == '03':
- contents[C] = int(input('input: '))
- step_forward = 2
- if DE == '04':
- print(C)
- step_forward = 2
- if DE == '05':
- if C != 0:
- x = contents[B]
- step_forward = 0
- else:
- step_forward = 3
- if DE == '06':
- if C == 0:
- x = contents[B]
- step_forward = 0
- else:
- step_forward = 3
- if DE == '07':
- contents[A] = C < B
- step_forward = 4
- if DE == '08':
- contents[A] = C == B
- step_forward = 4
- x += step_forward
- # part 1
- print("part 1")
- intcode(contents)
- # part 2
- print("\npart 2")
- intcode(contents)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement