# adventofcode 2019
# crushallhumans
# puzzle 5 - intcode part 2
# 12/5/2019

import sys
import unittest
from functools import reduce 

#DEBUG = True
DEBUG = False

def process_instructions(instructions, inputt = -1):
	i = 0
	test_c = 1
	diag_code = -1
	num_instrux = len(instructions)

	if DEBUG: print (f"instructions: {instructions}")
	if DEBUG: print (f"inputt: {inputt}")

	try:
		jump = 999999
		while i < num_instrux:
			if DEBUG: print (f"\n{i}: {instructions[i:i+20]}...")

			opcode = instructions[i]
			if DEBUG: print (f"opcode: {opcode}")

			# handle exit
			if (opcode == 99):
				jump = 0
				if DEBUG: print (" ___ 99 ___ \n\n")
				return (diag_code,instructions)

			# handle opcode
			else:
				vals = []
				jump = 4

				# handle parameterized opcode
				# get operational values
				# set cursor jump value
				if (opcode > 99):
					s_opcode = str(opcode)
					jump = len(s_opcode)

					# handle leading zeroes for non-IO opcodes
					if jump == 3 and opcode != 104 and opcode != 103:
						s_opcode = f"0{s_opcode}"
						jump = len(s_opcode)

					num_params = jump - 2
					opcode = int(s_opcode[-2:])

					if not (9 > opcode > 0):
						raise Exception('bad opcode encoded: {s_opcode}')

					if DEBUG: print (f"opcode: {opcode}")
					if DEBUG: print (f"s_opcode: {s_opcode}")
					if DEBUG: print (f"s_opcode[-2:]: {s_opcode[-2:]}")
					if DEBUG: print (f"processed jump: {jump}")

					# process parameters starting from left of 0N
					for d in range(0,num_params):
						pos = -2-(d+1)
						param = int(s_opcode[pos])
						inner_idx = i+d+1
						idx = instructions[inner_idx]

						if DEBUG: print (f"pos: {pos}")
						if DEBUG: print (f"param: {param}")
						if DEBUG: print (f"inner_idx: {inner_idx}")

						# position mode
						if param == 0:
							if DEBUG: print (f"val posmode {idx}: {idx}")
							vals.append(instructions[idx])
						# immediate mode
						elif param == 1:
							if DEBUG: print (f"val immmode RAW: {idx}")
							vals.append(idx)


				# handle unparameterized opcode value collection
				if not len(vals):
					if DEBUG: print (f"plain opcode {opcode} getting val +1 at {i+1}")

					# input code is always immediate
					if opcode == 3:
						vals.append(instructions[i+1])
					else:
						vals.append(instructions[instructions[i+1]])

					# non-IO codes get a second value
					if (opcode != 3 and opcode != 4):
						if DEBUG: print (f"plain opcode {opcode} getting val +2 at {i+2}")
						vals.append(instructions[instructions[i+2]])


				#PROCESS OPCODES
				# jump codes
				if (opcode == 5 or opcode == 6):
					if DEBUG: print (f"vals op5/6: {vals}")
					jump = 3
					if (opcode == 5):
						if vals[0] != 0:
							if DEBUG: print (f"JUMP TO : {vals[1]}")
							i = vals[1]
							continue
						else:
							pass
					elif (opcode == 6):
						if vals[0] == 0:
							if DEBUG: print (f"JUMP TO : {vals[1]}")
							i = vals[1]
							continue
						else:
							pass

				# output code
				elif (opcode == 4):
					jump = 2
					if DEBUG: print (f"vals op4: {vals}")
					diag_code = vals[0]
					test_c += 1

				# input code
				elif (opcode == 3):
					jump = 2
					if DEBUG: print (f"vals op3: {vals}")
					instructions[vals[0]] = inputt
					if DEBUG: print (f"write pos: {vals[0]}")
					if DEBUG: print (f"write val: {inputt}")
						
				# operation codes
				elif (opcode == 1 or opcode == 2 or opcode == 7 or opcode == 8):
					if DEBUG: print (f"vals op1/2/7/8: {vals}")
					result = -1
					if (opcode == 1):
						result = reduce((lambda x, y: x + y), vals)
					elif (opcode == 2):
						result = reduce((lambda x, y: x * y), vals)
					elif (opcode == 7):
						result = 1 if vals[0] < vals[1] else 0
					elif (opcode == 8):
						result = 1 if vals[0] == vals[1] else 0

					position = instructions[i+(jump-1)]
					instructions[position] = result

					if DEBUG: print (f"write pos: {position}")
					if DEBUG: print (f"write val: {result}")
				else:
					raise Exception('opcode not understood: ',opcode) 
			if DEBUG: print (f"jump: {jump}")
			i += jump
	except:
		raise Exception("Unknown exception")
	raise Exception("process_instructions finished organically")
	return False


class testCase(unittest.TestCase):
	def test_process_instructions(self):
		self.assertEqual(
			process_instructions([1,9,10,3,2,3,11,0,99,30,40,50])[1],
			[3500,9,10,70,2,3,11,0,99,30,40,50],
			"[1,9,10,3,2,3,11,0,99,30,40,50]"
		)
		self.assertEqual(process_instructions([1,0,0,0,99])[1], [2,0,0,0,99], [1,0,0,0,99])
		self.assertEqual(process_instructions([2,3,0,3,99])[1], [2,3,0,6,99], [2,3,0,3,99])
		self.assertEqual(process_instructions([2,4,4,5,99,0])[1], [2,4,4,5,99,9801], [2,4,4,5,99,0])
		self.assertEqual(process_instructions([1,1,1,4,99,5,6,0,99])[1], [30,1,1,4,2,5,6,0,99], [1,1,1,4,99,5,6,0,99])

		self.assertEqual(
			process_instructions([1002,4,3,4,33])[1], 
			[1002,4,3,4,99],
			"[1002,4,3,4,33]"
		)

		self.assertEqual(
			process_instructions([3,0,4,0,99],34939329)[0],
			34939329,
			"[3,0,4,0,99],34939329"
		)

		self.assertEqual(
			process_instructions(
				[3,9,8,9,10,9,4,9,99,-1,8],
				8
			)[0],
			1,
			"[3,9,8,9,10,9,4,9,99,-1,8],8"
		)

		self.assertEqual(
			process_instructions(
				[3,9,8,9,10,9,4,9,99,-1,8],
				3
			)[0],
			0,
			"[3,9,8,9,10,9,4,9,99,-1,8],3"
		)

		self.assertEqual(
			process_instructions(
				[3,3,1108,-1,8,3,4,3,99],
				8
			)[0],
			1,
			"[3,3,1108,-1,8,3,4,3,99],8"
		)

		self.assertEqual(
			process_instructions(
				[3,3,1108,-1,8,3,4,3,99],
				3
			)[0],
			0,
			"[3,3,1108,-1,8,3,4,3,99],3"
		)


		self.assertEqual(
			process_instructions(
				[3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9],
				0
			)[0],
			0,
			"[3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9],0"
		)
		self.assertEqual(
			process_instructions(
				[3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9],
				1291278
			)[0],
			1,
			"[3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9],1291278"
		)
		self.assertEqual(
			process_instructions(
				[3,3,1105,-1,9,1101,0,0,12,4,12,99,1],
				0
			)[0],
			0,
			"[3,3,1105,-1,9,1101,0,0,12,4,12,99,1],0"
		)
		self.assertEqual(
			process_instructions(
				[3,3,1105,-1,9,1101,0,0,12,4,12,99,1],
				3738281
			)[0],
			1,
			"[3,3,1105,-1,9,1101,0,0,12,4,12,99,1],3738281"
		)

		self.assertEqual(
			process_instructions(
				[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],
				5
			)[0],
			999,
			"[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],5"
		)
		self.assertEqual(
			process_instructions(
				[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],
				8
			)[0],
			1000,
			"[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],8"
		)
		self.assertEqual(
			process_instructions(
				[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],
				7347289
			)[0],
			1001,
			"[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],7347289"
		)





if __name__ == '__main__':
	try:
		sys.argv[1]
		puzzle_text()

	except:
		instructions = [3,225,1,225,6,6,1100,1,238,225,104,0,1001,191,50,224,101,-64,224,224,4,224,1002,223,8,223,101,5,224,224,1,224,223,223,2,150,218,224,1001,224,-1537,224,4,224,102,8,223,223,1001,224,2,224,1,223,224,223,1002,154,5,224,101,-35,224,224,4,224,1002,223,8,223,1001,224,5,224,1,224,223,223,1102,76,17,225,1102,21,44,224,1001,224,-924,224,4,224,102,8,223,223,1001,224,4,224,1,224,223,223,101,37,161,224,101,-70,224,224,4,224,1002,223,8,223,101,6,224,224,1,223,224,223,102,46,157,224,1001,224,-1978,224,4,224,102,8,223,223,1001,224,5,224,1,224,223,223,1102,5,29,225,1101,10,7,225,1101,43,38,225,1102,33,46,225,1,80,188,224,1001,224,-73,224,4,224,102,8,223,223,101,4,224,224,1,224,223,223,1101,52,56,225,1101,14,22,225,1101,66,49,224,1001,224,-115,224,4,224,1002,223,8,223,1001,224,7,224,1,224,223,223,1101,25,53,225,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,108,226,226,224,1002,223,2,223,1005,224,329,101,1,223,223,108,677,677,224,1002,223,2,223,1006,224,344,1001,223,1,223,8,677,677,224,102,2,223,223,1006,224,359,101,1,223,223,7,226,677,224,102,2,223,223,1005,224,374,101,1,223,223,107,226,226,224,102,2,223,223,1006,224,389,101,1,223,223,7,677,226,224,1002,223,2,223,1006,224,404,1001,223,1,223,1107,677,226,224,1002,223,2,223,1006,224,419,1001,223,1,223,1007,226,226,224,102,2,223,223,1005,224,434,101,1,223,223,1008,226,677,224,102,2,223,223,1005,224,449,1001,223,1,223,1007,677,677,224,1002,223,2,223,1006,224,464,1001,223,1,223,1008,226,226,224,102,2,223,223,1006,224,479,101,1,223,223,1007,226,677,224,1002,223,2,223,1005,224,494,1001,223,1,223,108,226,677,224,1002,223,2,223,1006,224,509,101,1,223,223,8,226,677,224,102,2,223,223,1005,224,524,1001,223,1,223,107,677,677,224,1002,223,2,223,1005,224,539,101,1,223,223,107,226,677,224,1002,223,2,223,1006,224,554,101,1,223,223,1107,226,677,224,1002,223,2,223,1006,224,569,1001,223,1,223,1108,677,226,224,102,2,223,223,1005,224,584,1001,223,1,223,1008,677,677,224,102,2,223,223,1005,224,599,1001,223,1,223,1107,677,677,224,102,2,223,223,1006,224,614,101,1,223,223,7,226,226,224,102,2,223,223,1005,224,629,1001,223,1,223,1108,677,677,224,102,2,223,223,1006,224,644,1001,223,1,223,8,677,226,224,1002,223,2,223,1005,224,659,101,1,223,223,1108,226,677,224,102,2,223,223,1005,224,674,101,1,223,223,4,223,99,226]

		# test A/C (1)
		f = process_instructions(instructions.copy(),1)
		print (f[0])	

		print()
		# test Thermal Radiators (5)
		f = process_instructions(instructions.copy(),5)
		print (f[0])	

	print("done");




def puzzle_text():
	print("""
--- Day 5: Sunny with a Chance of Asteroids ---
You're starting to sweat as the ship makes its way toward Mercury. The Elves suggest that you get the air conditioner working by upgrading your ship computer to support the Thermal Environment Supervision Terminal.

The Thermal Environment Supervision Terminal (TEST) starts by running a diagnostic program (your puzzle input). The TEST diagnostic program will run on your existing Intcode computer after a few modifications:

First, you'll need to add two new instructions:

Opcode 3 takes a single integer as input and saves it to the position given by its only parameter. For example, the instruction 3,50 would take an input value and store it at address 50.
Opcode 4 outputs the value of its only parameter. For example, the instruction 4,50 would output the value at address 50.
Programs that use these instructions will come with documentation that explains what should be connected to the input and output. The program 3,0,4,0,99 outputs whatever it gets as input, then halts.

Second, you'll need to add support for parameter modes:

Each parameter of an instruction is handled based on its parameter mode. Right now, your ship computer already understands parameter mode 0, position mode, which causes the parameter to be interpreted as a position - if the parameter is 50, its value is the value stored at address 50 in memory. Until now, all parameters have been in position mode.

Now, your ship computer will also need to handle parameters in mode 1, immediate mode. In immediate mode, a parameter is interpreted as a value - if the parameter is 50, its value is simply 50.

Parameter modes are stored in the same value as the instruction's opcode. The opcode is a two-digit number based only on the ones and tens digit of the value, that is, the opcode is the rightmost two digits of the first value in an instruction. Parameter modes are single digits, one per parameter, read right-to-left from the opcode: the first parameter's mode is in the hundreds digit, the second parameter's mode is in the thousands digit, the third parameter's mode is in the ten-thousands digit, and so on. Any missing modes are 0.

For example, consider the program 1002,4,3,4,33.

The first instruction, 1002,4,3,4, is a multiply instruction - the rightmost two digits of the first value, 02, indicate opcode 2, multiplication. Then, going right to left, the parameter modes are 0 (hundreds digit), 1 (thousands digit), and 0 (ten-thousands digit, not present and therefore zero):

ABCDE
 1002

DE - two-digit opcode,      02 == opcode 2
 C - mode of 1st parameter,  0 == position mode
 B - mode of 2nd parameter,  1 == immediate mode
 A - mode of 3rd parameter,  0 == position mode,
                                  omitted due to being a leading zero
This instruction multiplies its first two parameters. The first parameter, 4 in position mode, works like it did before - its value is the value stored at address 4 (33). The second parameter, 3 in immediate mode, simply has value 3. The result of this operation, 33 * 3 = 99, is written according to the third parameter, 4 in position mode, which also works like it did before - 99 is written to address 4.

Parameters that an instruction writes to will never be in immediate mode.

Finally, some notes:

It is important to remember that the instruction pointer should increase by the number of values in the instruction after the instruction finishes. Because of the new instructions, this amount is no longer always 4.
Integers can be negative: 1101,100,-1,4,0 is a valid program (find 100 + -1, store the result in position 4).
The TEST diagnostic program will start by requesting from the user the ID of the system to test by running an input instruction - provide it 1, the ID for the ship's air conditioner unit.

It will then perform a series of diagnostic tests confirming that various parts of the Intcode computer, like parameter modes, function correctly. For each test, it will run an output instruction indicating how far the result of the test was from the expected value, where 0 means the test was successful. Non-zero outputs mean that a function is not working correctly; check the instructions that were run before the output instruction to see which one failed.

Finally, the program will output a diagnostic code and immediately halt. This final output isn't an error; an output followed immediately by a halt means the program finished. If all outputs were zero except the diagnostic code, the diagnostic program ran successfully.

After providing 1 to the only input instruction and passing all the tests, what diagnostic code does the program produce?

Your puzzle answer was 11193703.

--- Part Two ---
The air conditioner comes online! Its cold air feels good for a while, but then the TEST alarms start to go off. Since the air conditioner can't vent its heat anywhere but back into the spacecraft, it's actually making the air inside the ship warmer.

Instead, you'll need to use the TEST to extend the thermal radiators. Fortunately, the diagnostic program (your puzzle input) is already equipped for this. Unfortunately, your Intcode computer is not.

Your computer is only missing a few opcodes:

Opcode 5 is jump-if-true: if the first parameter is non-zero, it sets the instruction pointer to the value from the second parameter. Otherwise, it does nothing.
Opcode 6 is jump-if-false: if the first parameter is zero, it sets the instruction pointer to the value from the second parameter. Otherwise, it does nothing.
Opcode 7 is less than: if the first parameter is less than the second parameter, it stores 1 in the position given by the third parameter. Otherwise, it stores 0.
Opcode 8 is equals: if the first parameter is equal to the second parameter, it stores 1 in the position given by the third parameter. Otherwise, it stores 0.
Like all instructions, these instructions need to support parameter modes as described above.

Normally, after an instruction is finished, the instruction pointer increases by the number of values in that instruction. However, if the instruction modifies the instruction pointer, that value is used and the instruction pointer is not automatically increased.

For example, here are several programs that take one input, compare it to the value 8, and then produce one output:

3,9,8,9,10,9,4,9,99,-1,8 - Using position mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
3,9,7,9,10,9,4,9,99,-1,8 - Using position mode, consider whether the input is less than 8; output 1 (if it is) or 0 (if it is not).
3,3,1108,-1,8,3,4,3,99 - Using immediate mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
3,3,1107,-1,8,3,4,3,99 - Using immediate mode, consider whether the input is less than 8; output 1 (if it is) or 0 (if it is not).
Here are some jump tests that take an input, then output 0 if the input was zero or 1 if the input was non-zero:

3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9 (using position mode)
3,3,1105,-1,9,1101,0,0,12,4,12,99,1 (using immediate mode)
Here's a larger example:

3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,
1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,
999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99
The above example program uses an input instruction to ask for a single number. The program will then output 999 if the input value is below 8, output 1000 if the input value is equal to 8, or output 1001 if the input value is greater than 8.

This time, when the TEST diagnostic program runs its input instruction to get the ID of the system to test, provide it 5, the ID for the ship's thermal radiator controller. This diagnostic test suite only outputs one number, the diagnostic code.

What is the diagnostic code for system ID 5?

Your puzzle answer was 12410607.

Both parts of this puzzle are complete! They provide two gold stars: **
""")

cec2b359375eba0bd9701ae961c76321bad9b57f
<mask token>


def process_instructions(instructions, inputt=-1):
    i = 0
    test_c = 1
    diag_code = -1
    num_instrux = len(instructions)
    if DEBUG:
        print(f'instructions: {instructions}')
    if DEBUG:
        print(f'inputt: {inputt}')
    try:
        jump = 999999
        while i < num_instrux:
            if DEBUG:
                print(f'\n{i}: {instructions[i:i + 20]}...')
            opcode = instructions[i]
            if DEBUG:
                print(f'opcode: {opcode}')
            if opcode == 99:
                jump = 0
                if DEBUG:
                    print(' ___ 99 ___ \n\n')
                return diag_code, instructions
            else:
                vals = []
                jump = 4
                if opcode > 99:
                    s_opcode = str(opcode)
                    jump = len(s_opcode)
                    if jump == 3 and opcode != 104 and opcode != 103:
                        s_opcode = f'0{s_opcode}'
                        jump = len(s_opcode)
                    num_params = jump - 2
                    opcode = int(s_opcode[-2:])
                    if not 9 > opcode > 0:
                        raise Exception('bad opcode encoded: {s_opcode}')
                    if DEBUG:
                        print(f'opcode: {opcode}')
                    if DEBUG:
                        print(f's_opcode: {s_opcode}')
                    if DEBUG:
                        print(f's_opcode[-2:]: {s_opcode[-2:]}')
                    if DEBUG:
                        print(f'processed jump: {jump}')
                    for d in range(0, num_params):
                        pos = -2 - (d + 1)
                        param = int(s_opcode[pos])
                        inner_idx = i + d + 1
                        idx = instructions[inner_idx]
                        if DEBUG:
                            print(f'pos: {pos}')
                        if DEBUG:
                            print(f'param: {param}')
                        if DEBUG:
                            print(f'inner_idx: {inner_idx}')
                        if param == 0:
                            if DEBUG:
                                print(f'val posmode {idx}: {idx}')
                            vals.append(instructions[idx])
                        elif param == 1:
                            if DEBUG:
                                print(f'val immmode RAW: {idx}')
                            vals.append(idx)
                if not len(vals):
                    if DEBUG:
                        print(
                            f'plain opcode {opcode} getting val +1 at {i + 1}')
                    if opcode == 3:
                        vals.append(instructions[i + 1])
                    else:
                        vals.append(instructions[instructions[i + 1]])
                    if opcode != 3 and opcode != 4:
                        if DEBUG:
                            print(
                                f'plain opcode {opcode} getting val +2 at {i + 2}'
                                )
                        vals.append(instructions[instructions[i + 2]])
                if opcode == 5 or opcode == 6:
                    if DEBUG:
                        print(f'vals op5/6: {vals}')
                    jump = 3
                    if opcode == 5:
                        if vals[0] != 0:
                            if DEBUG:
                                print(f'JUMP TO : {vals[1]}')
                            i = vals[1]
                            continue
                        else:
                            pass
                    elif opcode == 6:
                        if vals[0] == 0:
                            if DEBUG:
                                print(f'JUMP TO : {vals[1]}')
                            i = vals[1]
                            continue
                        else:
                            pass
                elif opcode == 4:
                    jump = 2
                    if DEBUG:
                        print(f'vals op4: {vals}')
                    diag_code = vals[0]
                    test_c += 1
                elif opcode == 3:
                    jump = 2
                    if DEBUG:
                        print(f'vals op3: {vals}')
                    instructions[vals[0]] = inputt
                    if DEBUG:
                        print(f'write pos: {vals[0]}')
                    if DEBUG:
                        print(f'write val: {inputt}')
                elif opcode == 1 or opcode == 2 or opcode == 7 or opcode == 8:
                    if DEBUG:
                        print(f'vals op1/2/7/8: {vals}')
                    result = -1
                    if opcode == 1:
                        result = reduce(lambda x, y: x + y, vals)
                    elif opcode == 2:
                        result = reduce(lambda x, y: x * y, vals)
                    elif opcode == 7:
                        result = 1 if vals[0] < vals[1] else 0
                    elif opcode == 8:
                        result = 1 if vals[0] == vals[1] else 0
                    position = instructions[i + (jump - 1)]
                    instructions[position] = result
                    if DEBUG:
                        print(f'write pos: {position}')
                    if DEBUG:
                        print(f'write val: {result}')
                else:
                    raise Exception('opcode not understood: ', opcode)
            if DEBUG:
                print(f'jump: {jump}')
            i += jump
    except:
        raise Exception('Unknown exception')
    raise Exception('process_instructions finished organically')
    return False


class testCase(unittest.TestCase):

    def test_process_instructions(self):
        self.assertEqual(process_instructions([1, 9, 10, 3, 2, 3, 11, 0, 99,
            30, 40, 50])[1], [3500, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50],
            '[1,9,10,3,2,3,11,0,99,30,40,50]')
        self.assertEqual(process_instructions([1, 0, 0, 0, 99])[1], [2, 0, 
            0, 0, 99], [1, 0, 0, 0, 99])
        self.assertEqual(process_instructions([2, 3, 0, 3, 99])[1], [2, 3, 
            0, 6, 99], [2, 3, 0, 3, 99])
        self.assertEqual(process_instructions([2, 4, 4, 5, 99, 0])[1], [2, 
            4, 4, 5, 99, 9801], [2, 4, 4, 5, 99, 0])
        self.assertEqual(process_instructions([1, 1, 1, 4, 99, 5, 6, 0, 99]
            )[1], [30, 1, 1, 4, 2, 5, 6, 0, 99], [1, 1, 1, 4, 99, 5, 6, 0, 99])
        self.assertEqual(process_instructions([1002, 4, 3, 4, 33])[1], [
            1002, 4, 3, 4, 99], '[1002,4,3,4,33]')
        self.assertEqual(process_instructions([3, 0, 4, 0, 99], 34939329)[0
            ], 34939329, '[3,0,4,0,99],34939329')
        self.assertEqual(process_instructions([3, 9, 8, 9, 10, 9, 4, 9, 99,
            -1, 8], 8)[0], 1, '[3,9,8,9,10,9,4,9,99,-1,8],8')
        self.assertEqual(process_instructions([3, 9, 8, 9, 10, 9, 4, 9, 99,
            -1, 8], 3)[0], 0, '[3,9,8,9,10,9,4,9,99,-1,8],3')
        self.assertEqual(process_instructions([3, 3, 1108, -1, 8, 3, 4, 3, 
            99], 8)[0], 1, '[3,3,1108,-1,8,3,4,3,99],8')
        self.assertEqual(process_instructions([3, 3, 1108, -1, 8, 3, 4, 3, 
            99], 3)[0], 0, '[3,3,1108,-1,8,3,4,3,99],3')
        self.assertEqual(process_instructions([3, 12, 6, 12, 15, 1, 13, 14,
            13, 4, 13, 99, -1, 0, 1, 9], 0)[0], 0,
            '[3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9],0')
        self.assertEqual(process_instructions([3, 12, 6, 12, 15, 1, 13, 14,
            13, 4, 13, 99, -1, 0, 1, 9], 1291278)[0], 1,
            '[3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9],1291278')
        self.assertEqual(process_instructions([3, 3, 1105, -1, 9, 1101, 0, 
            0, 12, 4, 12, 99, 1], 0)[0], 0,
            '[3,3,1105,-1,9,1101,0,0,12,4,12,99,1],0')
        self.assertEqual(process_instructions([3, 3, 1105, -1, 9, 1101, 0, 
            0, 12, 4, 12, 99, 1], 3738281)[0], 1,
            '[3,3,1105,-1,9,1101,0,0,12,4,12,99,1],3738281')
        self.assertEqual(process_instructions([3, 21, 1008, 21, 8, 20, 1005,
            20, 22, 107, 8, 21, 20, 1006, 20, 31, 1106, 0, 36, 98, 0, 0, 
            1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104, 999, 1105, 1, 46, 
            1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99], 5)[0], 999,
            '[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],5'
            )
        self.assertEqual(process_instructions([3, 21, 1008, 21, 8, 20, 1005,
            20, 22, 107, 8, 21, 20, 1006, 20, 31, 1106, 0, 36, 98, 0, 0, 
            1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104, 999, 1105, 1, 46, 
            1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99], 8)[0], 1000,
            '[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],8'
            )
        self.assertEqual(process_instructions([3, 21, 1008, 21, 8, 20, 1005,
            20, 22, 107, 8, 21, 20, 1006, 20, 31, 1106, 0, 36, 98, 0, 0, 
            1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104, 999, 1105, 1, 46, 
            1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99], 7347289)[0], 
            1001,
            '[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],7347289'
            )


<mask token>


def puzzle_text():
    print(
        """
--- Day 5: Sunny with a Chance of Asteroids ---
You're starting to sweat as the ship makes its way toward Mercury. The Elves suggest that you get the air conditioner working by upgrading your ship computer to support the Thermal Environment Supervision Terminal.

The Thermal Environment Supervision Terminal (TEST) starts by running a diagnostic program (your puzzle input). The TEST diagnostic program will run on your existing Intcode computer after a few modifications:

First, you'll need to add two new instructions:

Opcode 3 takes a single integer as input and saves it to the position given by its only parameter. For example, the instruction 3,50 would take an input value and store it at address 50.
Opcode 4 outputs the value of its only parameter. For example, the instruction 4,50 would output the value at address 50.
Programs that use these instructions will come with documentation that explains what should be connected to the input and output. The program 3,0,4,0,99 outputs whatever it gets as input, then halts.

Second, you'll need to add support for parameter modes:

Each parameter of an instruction is handled based on its parameter mode. Right now, your ship computer already understands parameter mode 0, position mode, which causes the parameter to be interpreted as a position - if the parameter is 50, its value is the value stored at address 50 in memory. Until now, all parameters have been in position mode.

Now, your ship computer will also need to handle parameters in mode 1, immediate mode. In immediate mode, a parameter is interpreted as a value - if the parameter is 50, its value is simply 50.

Parameter modes are stored in the same value as the instruction's opcode. The opcode is a two-digit number based only on the ones and tens digit of the value, that is, the opcode is the rightmost two digits of the first value in an instruction. Parameter modes are single digits, one per parameter, read right-to-left from the opcode: the first parameter's mode is in the hundreds digit, the second parameter's mode is in the thousands digit, the third parameter's mode is in the ten-thousands digit, and so on. Any missing modes are 0.

For example, consider the program 1002,4,3,4,33.

The first instruction, 1002,4,3,4, is a multiply instruction - the rightmost two digits of the first value, 02, indicate opcode 2, multiplication. Then, going right to left, the parameter modes are 0 (hundreds digit), 1 (thousands digit), and 0 (ten-thousands digit, not present and therefore zero):

ABCDE
 1002

DE - two-digit opcode,      02 == opcode 2
 C - mode of 1st parameter,  0 == position mode
 B - mode of 2nd parameter,  1 == immediate mode
 A - mode of 3rd parameter,  0 == position mode,
                                  omitted due to being a leading zero
This instruction multiplies its first two parameters. The first parameter, 4 in position mode, works like it did before - its value is the value stored at address 4 (33). The second parameter, 3 in immediate mode, simply has value 3. The result of this operation, 33 * 3 = 99, is written according to the third parameter, 4 in position mode, which also works like it did before - 99 is written to address 4.

Parameters that an instruction writes to will never be in immediate mode.

Finally, some notes:

It is important to remember that the instruction pointer should increase by the number of values in the instruction after the instruction finishes. Because of the new instructions, this amount is no longer always 4.
Integers can be negative: 1101,100,-1,4,0 is a valid program (find 100 + -1, store the result in position 4).
The TEST diagnostic program will start by requesting from the user the ID of the system to test by running an input instruction - provide it 1, the ID for the ship's air conditioner unit.

It will then perform a series of diagnostic tests confirming that various parts of the Intcode computer, like parameter modes, function correctly. For each test, it will run an output instruction indicating how far the result of the test was from the expected value, where 0 means the test was successful. Non-zero outputs mean that a function is not working correctly; check the instructions that were run before the output instruction to see which one failed.

Finally, the program will output a diagnostic code and immediately halt. This final output isn't an error; an output followed immediately by a halt means the program finished. If all outputs were zero except the diagnostic code, the diagnostic program ran successfully.

After providing 1 to the only input instruction and passing all the tests, what diagnostic code does the program produce?

Your puzzle answer was 11193703.

--- Part Two ---
The air conditioner comes online! Its cold air feels good for a while, but then the TEST alarms start to go off. Since the air conditioner can't vent its heat anywhere but back into the spacecraft, it's actually making the air inside the ship warmer.

Instead, you'll need to use the TEST to extend the thermal radiators. Fortunately, the diagnostic program (your puzzle input) is already equipped for this. Unfortunately, your Intcode computer is not.

Your computer is only missing a few opcodes:

Opcode 5 is jump-if-true: if the first parameter is non-zero, it sets the instruction pointer to the value from the second parameter. Otherwise, it does nothing.
Opcode 6 is jump-if-false: if the first parameter is zero, it sets the instruction pointer to the value from the second parameter. Otherwise, it does nothing.
Opcode 7 is less than: if the first parameter is less than the second parameter, it stores 1 in the position given by the third parameter. Otherwise, it stores 0.
Opcode 8 is equals: if the first parameter is equal to the second parameter, it stores 1 in the position given by the third parameter. Otherwise, it stores 0.
Like all instructions, these instructions need to support parameter modes as described above.

Normally, after an instruction is finished, the instruction pointer increases by the number of values in that instruction. However, if the instruction modifies the instruction pointer, that value is used and the instruction pointer is not automatically increased.

For example, here are several programs that take one input, compare it to the value 8, and then produce one output:

3,9,8,9,10,9,4,9,99,-1,8 - Using position mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
3,9,7,9,10,9,4,9,99,-1,8 - Using position mode, consider whether the input is less than 8; output 1 (if it is) or 0 (if it is not).
3,3,1108,-1,8,3,4,3,99 - Using immediate mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
3,3,1107,-1,8,3,4,3,99 - Using immediate mode, consider whether the input is less than 8; output 1 (if it is) or 0 (if it is not).
Here are some jump tests that take an input, then output 0 if the input was zero or 1 if the input was non-zero:

3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9 (using position mode)
3,3,1105,-1,9,1101,0,0,12,4,12,99,1 (using immediate mode)
Here's a larger example:

3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,
1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,
999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99
The above example program uses an input instruction to ask for a single number. The program will then output 999 if the input value is below 8, output 1000 if the input value is equal to 8, or output 1001 if the input value is greater than 8.

This time, when the TEST diagnostic program runs its input instruction to get the ID of the system to test, provide it 5, the ID for the ship's thermal radiator controller. This diagnostic test suite only outputs one number, the diagnostic code.

What is the diagnostic code for system ID 5?

Your puzzle answer was 12410607.

Both parts of this puzzle are complete! They provide two gold stars: **
"""
        )

<mask token>


def process_instructions(instructions, inputt=-1):
    i = 0
    test_c = 1
    diag_code = -1
    num_instrux = len(instructions)
    if DEBUG:
        print(f'instructions: {instructions}')
    if DEBUG:
        print(f'inputt: {inputt}')
    try:
        jump = 999999
        while i < num_instrux:
            if DEBUG:
                print(f'\n{i}: {instructions[i:i + 20]}...')
            opcode = instructions[i]
            if DEBUG:
                print(f'opcode: {opcode}')
            if opcode == 99:
                jump = 0
                if DEBUG:
                    print(' ___ 99 ___ \n\n')
                return diag_code, instructions
            else:
                vals = []
                jump = 4
                if opcode > 99:
                    s_opcode = str(opcode)
                    jump = len(s_opcode)
                    if jump == 3 and opcode != 104 and opcode != 103:
                        s_opcode = f'0{s_opcode}'
                        jump = len(s_opcode)
                    num_params = jump - 2
                    opcode = int(s_opcode[-2:])
                    if not 9 > opcode > 0:
                        raise Exception('bad opcode encoded: {s_opcode}')
                    if DEBUG:
                        print(f'opcode: {opcode}')
                    if DEBUG:
                        print(f's_opcode: {s_opcode}')
                    if DEBUG:
                        print(f's_opcode[-2:]: {s_opcode[-2:]}')
                    if DEBUG:
                        print(f'processed jump: {jump}')
                    for d in range(0, num_params):
                        pos = -2 - (d + 1)
                        param = int(s_opcode[pos])
                        inner_idx = i + d + 1
                        idx = instructions[inner_idx]
                        if DEBUG:
                            print(f'pos: {pos}')
                        if DEBUG:
                            print(f'param: {param}')
                        if DEBUG:
                            print(f'inner_idx: {inner_idx}')
                        if param == 0:
                            if DEBUG:
                                print(f'val posmode {idx}: {idx}')
                            vals.append(instructions[idx])
                        elif param == 1:
                            if DEBUG:
                                print(f'val immmode RAW: {idx}')
                            vals.append(idx)
                if not len(vals):
                    if DEBUG:
                        print(
                            f'plain opcode {opcode} getting val +1 at {i + 1}')
                    if opcode == 3:
                        vals.append(instructions[i + 1])
                    else:
                        vals.append(instructions[instructions[i + 1]])
                    if opcode != 3 and opcode != 4:
                        if DEBUG:
                            print(
                                f'plain opcode {opcode} getting val +2 at {i + 2}'
                                )
                        vals.append(instructions[instructions[i + 2]])
                if opcode == 5 or opcode == 6:
                    if DEBUG:
                        print(f'vals op5/6: {vals}')
                    jump = 3
                    if opcode == 5:
                        if vals[0] != 0:
                            if DEBUG:
                                print(f'JUMP TO : {vals[1]}')
                            i = vals[1]
                            continue
                        else:
                            pass
                    elif opcode == 6:
                        if vals[0] == 0:
                            if DEBUG:
                                print(f'JUMP TO : {vals[1]}')
                            i = vals[1]
                            continue
                        else:
                            pass
                elif opcode == 4:
                    jump = 2
                    if DEBUG:
                        print(f'vals op4: {vals}')
                    diag_code = vals[0]
                    test_c += 1
                elif opcode == 3:
                    jump = 2
                    if DEBUG:
                        print(f'vals op3: {vals}')
                    instructions[vals[0]] = inputt
                    if DEBUG:
                        print(f'write pos: {vals[0]}')
                    if DEBUG:
                        print(f'write val: {inputt}')
                elif opcode == 1 or opcode == 2 or opcode == 7 or opcode == 8:
                    if DEBUG:
                        print(f'vals op1/2/7/8: {vals}')
                    result = -1
                    if opcode == 1:
                        result = reduce(lambda x, y: x + y, vals)
                    elif opcode == 2:
                        result = reduce(lambda x, y: x * y, vals)
                    elif opcode == 7:
                        result = 1 if vals[0] < vals[1] else 0
                    elif opcode == 8:
                        result = 1 if vals[0] == vals[1] else 0
                    position = instructions[i + (jump - 1)]
                    instructions[position] = result
                    if DEBUG:
                        print(f'write pos: {position}')
                    if DEBUG:
                        print(f'write val: {result}')
                else:
                    raise Exception('opcode not understood: ', opcode)
            if DEBUG:
                print(f'jump: {jump}')
            i += jump
    except:
        raise Exception('Unknown exception')
    raise Exception('process_instructions finished organically')
    return False


class testCase(unittest.TestCase):

    def test_process_instructions(self):
        self.assertEqual(process_instructions([1, 9, 10, 3, 2, 3, 11, 0, 99,
            30, 40, 50])[1], [3500, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50],
            '[1,9,10,3,2,3,11,0,99,30,40,50]')
        self.assertEqual(process_instructions([1, 0, 0, 0, 99])[1], [2, 0, 
            0, 0, 99], [1, 0, 0, 0, 99])
        self.assertEqual(process_instructions([2, 3, 0, 3, 99])[1], [2, 3, 
            0, 6, 99], [2, 3, 0, 3, 99])
        self.assertEqual(process_instructions([2, 4, 4, 5, 99, 0])[1], [2, 
            4, 4, 5, 99, 9801], [2, 4, 4, 5, 99, 0])
        self.assertEqual(process_instructions([1, 1, 1, 4, 99, 5, 6, 0, 99]
            )[1], [30, 1, 1, 4, 2, 5, 6, 0, 99], [1, 1, 1, 4, 99, 5, 6, 0, 99])
        self.assertEqual(process_instructions([1002, 4, 3, 4, 33])[1], [
            1002, 4, 3, 4, 99], '[1002,4,3,4,33]')
        self.assertEqual(process_instructions([3, 0, 4, 0, 99], 34939329)[0
            ], 34939329, '[3,0,4,0,99],34939329')
        self.assertEqual(process_instructions([3, 9, 8, 9, 10, 9, 4, 9, 99,
            -1, 8], 8)[0], 1, '[3,9,8,9,10,9,4,9,99,-1,8],8')
        self.assertEqual(process_instructions([3, 9, 8, 9, 10, 9, 4, 9, 99,
            -1, 8], 3)[0], 0, '[3,9,8,9,10,9,4,9,99,-1,8],3')
        self.assertEqual(process_instructions([3, 3, 1108, -1, 8, 3, 4, 3, 
            99], 8)[0], 1, '[3,3,1108,-1,8,3,4,3,99],8')
        self.assertEqual(process_instructions([3, 3, 1108, -1, 8, 3, 4, 3, 
            99], 3)[0], 0, '[3,3,1108,-1,8,3,4,3,99],3')
        self.assertEqual(process_instructions([3, 12, 6, 12, 15, 1, 13, 14,
            13, 4, 13, 99, -1, 0, 1, 9], 0)[0], 0,
            '[3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9],0')
        self.assertEqual(process_instructions([3, 12, 6, 12, 15, 1, 13, 14,
            13, 4, 13, 99, -1, 0, 1, 9], 1291278)[0], 1,
            '[3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9],1291278')
        self.assertEqual(process_instructions([3, 3, 1105, -1, 9, 1101, 0, 
            0, 12, 4, 12, 99, 1], 0)[0], 0,
            '[3,3,1105,-1,9,1101,0,0,12,4,12,99,1],0')
        self.assertEqual(process_instructions([3, 3, 1105, -1, 9, 1101, 0, 
            0, 12, 4, 12, 99, 1], 3738281)[0], 1,
            '[3,3,1105,-1,9,1101,0,0,12,4,12,99,1],3738281')
        self.assertEqual(process_instructions([3, 21, 1008, 21, 8, 20, 1005,
            20, 22, 107, 8, 21, 20, 1006, 20, 31, 1106, 0, 36, 98, 0, 0, 
            1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104, 999, 1105, 1, 46, 
            1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99], 5)[0], 999,
            '[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],5'
            )
        self.assertEqual(process_instructions([3, 21, 1008, 21, 8, 20, 1005,
            20, 22, 107, 8, 21, 20, 1006, 20, 31, 1106, 0, 36, 98, 0, 0, 
            1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104, 999, 1105, 1, 46, 
            1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99], 8)[0], 1000,
            '[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],8'
            )
        self.assertEqual(process_instructions([3, 21, 1008, 21, 8, 20, 1005,
            20, 22, 107, 8, 21, 20, 1006, 20, 31, 1106, 0, 36, 98, 0, 0, 
            1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104, 999, 1105, 1, 46, 
            1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99], 7347289)[0], 
            1001,
            '[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],7347289'
            )


if __name__ == '__main__':
    try:
        sys.argv[1]
        puzzle_text()
    except:
        instructions = [3, 225, 1, 225, 6, 6, 1100, 1, 238, 225, 104, 0, 
            1001, 191, 50, 224, 101, -64, 224, 224, 4, 224, 1002, 223, 8, 
            223, 101, 5, 224, 224, 1, 224, 223, 223, 2, 150, 218, 224, 1001,
            224, -1537, 224, 4, 224, 102, 8, 223, 223, 1001, 224, 2, 224, 1,
            223, 224, 223, 1002, 154, 5, 224, 101, -35, 224, 224, 4, 224, 
            1002, 223, 8, 223, 1001, 224, 5, 224, 1, 224, 223, 223, 1102, 
            76, 17, 225, 1102, 21, 44, 224, 1001, 224, -924, 224, 4, 224, 
            102, 8, 223, 223, 1001, 224, 4, 224, 1, 224, 223, 223, 101, 37,
            161, 224, 101, -70, 224, 224, 4, 224, 1002, 223, 8, 223, 101, 6,
            224, 224, 1, 223, 224, 223, 102, 46, 157, 224, 1001, 224, -1978,
            224, 4, 224, 102, 8, 223, 223, 1001, 224, 5, 224, 1, 224, 223, 
            223, 1102, 5, 29, 225, 1101, 10, 7, 225, 1101, 43, 38, 225, 
            1102, 33, 46, 225, 1, 80, 188, 224, 1001, 224, -73, 224, 4, 224,
            102, 8, 223, 223, 101, 4, 224, 224, 1, 224, 223, 223, 1101, 52,
            56, 225, 1101, 14, 22, 225, 1101, 66, 49, 224, 1001, 224, -115,
            224, 4, 224, 1002, 223, 8, 223, 1001, 224, 7, 224, 1, 224, 223,
            223, 1101, 25, 53, 225, 4, 223, 99, 0, 0, 0, 677, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 1105, 0, 99999, 1105, 227, 247, 1105, 1, 
            99999, 1005, 227, 99999, 1005, 0, 256, 1105, 1, 99999, 1106, 
            227, 99999, 1106, 0, 265, 1105, 1, 99999, 1006, 0, 99999, 1006,
            227, 274, 1105, 1, 99999, 1105, 1, 280, 1105, 1, 99999, 1, 225,
            225, 225, 1101, 294, 0, 0, 105, 1, 0, 1105, 1, 99999, 1106, 0, 
            300, 1105, 1, 99999, 1, 225, 225, 225, 1101, 314, 0, 0, 106, 0,
            0, 1105, 1, 99999, 108, 226, 226, 224, 1002, 223, 2, 223, 1005,
            224, 329, 101, 1, 223, 223, 108, 677, 677, 224, 1002, 223, 2, 
            223, 1006, 224, 344, 1001, 223, 1, 223, 8, 677, 677, 224, 102, 
            2, 223, 223, 1006, 224, 359, 101, 1, 223, 223, 7, 226, 677, 224,
            102, 2, 223, 223, 1005, 224, 374, 101, 1, 223, 223, 107, 226, 
            226, 224, 102, 2, 223, 223, 1006, 224, 389, 101, 1, 223, 223, 7,
            677, 226, 224, 1002, 223, 2, 223, 1006, 224, 404, 1001, 223, 1,
            223, 1107, 677, 226, 224, 1002, 223, 2, 223, 1006, 224, 419, 
            1001, 223, 1, 223, 1007, 226, 226, 224, 102, 2, 223, 223, 1005,
            224, 434, 101, 1, 223, 223, 1008, 226, 677, 224, 102, 2, 223, 
            223, 1005, 224, 449, 1001, 223, 1, 223, 1007, 677, 677, 224, 
            1002, 223, 2, 223, 1006, 224, 464, 1001, 223, 1, 223, 1008, 226,
            226, 224, 102, 2, 223, 223, 1006, 224, 479, 101, 1, 223, 223, 
            1007, 226, 677, 224, 1002, 223, 2, 223, 1005, 224, 494, 1001, 
            223, 1, 223, 108, 226, 677, 224, 1002, 223, 2, 223, 1006, 224, 
            509, 101, 1, 223, 223, 8, 226, 677, 224, 102, 2, 223, 223, 1005,
            224, 524, 1001, 223, 1, 223, 107, 677, 677, 224, 1002, 223, 2, 
            223, 1005, 224, 539, 101, 1, 223, 223, 107, 226, 677, 224, 1002,
            223, 2, 223, 1006, 224, 554, 101, 1, 223, 223, 1107, 226, 677, 
            224, 1002, 223, 2, 223, 1006, 224, 569, 1001, 223, 1, 223, 1108,
            677, 226, 224, 102, 2, 223, 223, 1005, 224, 584, 1001, 223, 1, 
            223, 1008, 677, 677, 224, 102, 2, 223, 223, 1005, 224, 599, 
            1001, 223, 1, 223, 1107, 677, 677, 224, 102, 2, 223, 223, 1006,
            224, 614, 101, 1, 223, 223, 7, 226, 226, 224, 102, 2, 223, 223,
            1005, 224, 629, 1001, 223, 1, 223, 1108, 677, 677, 224, 102, 2,
            223, 223, 1006, 224, 644, 1001, 223, 1, 223, 8, 677, 226, 224, 
            1002, 223, 2, 223, 1005, 224, 659, 101, 1, 223, 223, 1108, 226,
            677, 224, 102, 2, 223, 223, 1005, 224, 674, 101, 1, 223, 223, 4,
            223, 99, 226]
        f = process_instructions(instructions.copy(), 1)
        print(f[0])
        print()
        f = process_instructions(instructions.copy(), 5)
        print(f[0])
    print('done')


def puzzle_text():
    print(
        """
--- Day 5: Sunny with a Chance of Asteroids ---
You're starting to sweat as the ship makes its way toward Mercury. The Elves suggest that you get the air conditioner working by upgrading your ship computer to support the Thermal Environment Supervision Terminal.

The Thermal Environment Supervision Terminal (TEST) starts by running a diagnostic program (your puzzle input). The TEST diagnostic program will run on your existing Intcode computer after a few modifications:

First, you'll need to add two new instructions:

Opcode 3 takes a single integer as input and saves it to the position given by its only parameter. For example, the instruction 3,50 would take an input value and store it at address 50.
Opcode 4 outputs the value of its only parameter. For example, the instruction 4,50 would output the value at address 50.
Programs that use these instructions will come with documentation that explains what should be connected to the input and output. The program 3,0,4,0,99 outputs whatever it gets as input, then halts.

Second, you'll need to add support for parameter modes:

Each parameter of an instruction is handled based on its parameter mode. Right now, your ship computer already understands parameter mode 0, position mode, which causes the parameter to be interpreted as a position - if the parameter is 50, its value is the value stored at address 50 in memory. Until now, all parameters have been in position mode.

Now, your ship computer will also need to handle parameters in mode 1, immediate mode. In immediate mode, a parameter is interpreted as a value - if the parameter is 50, its value is simply 50.

Parameter modes are stored in the same value as the instruction's opcode. The opcode is a two-digit number based only on the ones and tens digit of the value, that is, the opcode is the rightmost two digits of the first value in an instruction. Parameter modes are single digits, one per parameter, read right-to-left from the opcode: the first parameter's mode is in the hundreds digit, the second parameter's mode is in the thousands digit, the third parameter's mode is in the ten-thousands digit, and so on. Any missing modes are 0.

For example, consider the program 1002,4,3,4,33.

The first instruction, 1002,4,3,4, is a multiply instruction - the rightmost two digits of the first value, 02, indicate opcode 2, multiplication. Then, going right to left, the parameter modes are 0 (hundreds digit), 1 (thousands digit), and 0 (ten-thousands digit, not present and therefore zero):

ABCDE
 1002

DE - two-digit opcode,      02 == opcode 2
 C - mode of 1st parameter,  0 == position mode
 B - mode of 2nd parameter,  1 == immediate mode
 A - mode of 3rd parameter,  0 == position mode,
                                  omitted due to being a leading zero
This instruction multiplies its first two parameters. The first parameter, 4 in position mode, works like it did before - its value is the value stored at address 4 (33). The second parameter, 3 in immediate mode, simply has value 3. The result of this operation, 33 * 3 = 99, is written according to the third parameter, 4 in position mode, which also works like it did before - 99 is written to address 4.

Parameters that an instruction writes to will never be in immediate mode.

Finally, some notes:

It is important to remember that the instruction pointer should increase by the number of values in the instruction after the instruction finishes. Because of the new instructions, this amount is no longer always 4.
Integers can be negative: 1101,100,-1,4,0 is a valid program (find 100 + -1, store the result in position 4).
The TEST diagnostic program will start by requesting from the user the ID of the system to test by running an input instruction - provide it 1, the ID for the ship's air conditioner unit.

It will then perform a series of diagnostic tests confirming that various parts of the Intcode computer, like parameter modes, function correctly. For each test, it will run an output instruction indicating how far the result of the test was from the expected value, where 0 means the test was successful. Non-zero outputs mean that a function is not working correctly; check the instructions that were run before the output instruction to see which one failed.

Finally, the program will output a diagnostic code and immediately halt. This final output isn't an error; an output followed immediately by a halt means the program finished. If all outputs were zero except the diagnostic code, the diagnostic program ran successfully.

After providing 1 to the only input instruction and passing all the tests, what diagnostic code does the program produce?

Your puzzle answer was 11193703.

--- Part Two ---
The air conditioner comes online! Its cold air feels good for a while, but then the TEST alarms start to go off. Since the air conditioner can't vent its heat anywhere but back into the spacecraft, it's actually making the air inside the ship warmer.

Instead, you'll need to use the TEST to extend the thermal radiators. Fortunately, the diagnostic program (your puzzle input) is already equipped for this. Unfortunately, your Intcode computer is not.

Your computer is only missing a few opcodes:

Opcode 5 is jump-if-true: if the first parameter is non-zero, it sets the instruction pointer to the value from the second parameter. Otherwise, it does nothing.
Opcode 6 is jump-if-false: if the first parameter is zero, it sets the instruction pointer to the value from the second parameter. Otherwise, it does nothing.
Opcode 7 is less than: if the first parameter is less than the second parameter, it stores 1 in the position given by the third parameter. Otherwise, it stores 0.
Opcode 8 is equals: if the first parameter is equal to the second parameter, it stores 1 in the position given by the third parameter. Otherwise, it stores 0.
Like all instructions, these instructions need to support parameter modes as described above.

Normally, after an instruction is finished, the instruction pointer increases by the number of values in that instruction. However, if the instruction modifies the instruction pointer, that value is used and the instruction pointer is not automatically increased.

For example, here are several programs that take one input, compare it to the value 8, and then produce one output:

3,9,8,9,10,9,4,9,99,-1,8 - Using position mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
3,9,7,9,10,9,4,9,99,-1,8 - Using position mode, consider whether the input is less than 8; output 1 (if it is) or 0 (if it is not).
3,3,1108,-1,8,3,4,3,99 - Using immediate mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
3,3,1107,-1,8,3,4,3,99 - Using immediate mode, consider whether the input is less than 8; output 1 (if it is) or 0 (if it is not).
Here are some jump tests that take an input, then output 0 if the input was zero or 1 if the input was non-zero:

3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9 (using position mode)
3,3,1105,-1,9,1101,0,0,12,4,12,99,1 (using immediate mode)
Here's a larger example:

3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,
1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,
999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99
The above example program uses an input instruction to ask for a single number. The program will then output 999 if the input value is below 8, output 1000 if the input value is equal to 8, or output 1001 if the input value is greater than 8.

This time, when the TEST diagnostic program runs its input instruction to get the ID of the system to test, provide it 5, the ID for the ship's thermal radiator controller. This diagnostic test suite only outputs one number, the diagnostic code.

What is the diagnostic code for system ID 5?

Your puzzle answer was 12410607.

Both parts of this puzzle are complete! They provide two gold stars: **
"""
        )

<mask token>
DEBUG = False


def process_instructions(instructions, inputt=-1):
    i = 0
    test_c = 1
    diag_code = -1
    num_instrux = len(instructions)
    if DEBUG:
        print(f'instructions: {instructions}')
    if DEBUG:
        print(f'inputt: {inputt}')
    try:
        jump = 999999
        while i < num_instrux:
            if DEBUG:
                print(f'\n{i}: {instructions[i:i + 20]}...')
            opcode = instructions[i]
            if DEBUG:
                print(f'opcode: {opcode}')
            if opcode == 99:
                jump = 0
                if DEBUG:
                    print(' ___ 99 ___ \n\n')
                return diag_code, instructions
            else:
                vals = []
                jump = 4
                if opcode > 99:
                    s_opcode = str(opcode)
                    jump = len(s_opcode)
                    if jump == 3 and opcode != 104 and opcode != 103:
                        s_opcode = f'0{s_opcode}'
                        jump = len(s_opcode)
                    num_params = jump - 2
                    opcode = int(s_opcode[-2:])
                    if not 9 > opcode > 0:
                        raise Exception('bad opcode encoded: {s_opcode}')
                    if DEBUG:
                        print(f'opcode: {opcode}')
                    if DEBUG:
                        print(f's_opcode: {s_opcode}')
                    if DEBUG:
                        print(f's_opcode[-2:]: {s_opcode[-2:]}')
                    if DEBUG:
                        print(f'processed jump: {jump}')
                    for d in range(0, num_params):
                        pos = -2 - (d + 1)
                        param = int(s_opcode[pos])
                        inner_idx = i + d + 1
                        idx = instructions[inner_idx]
                        if DEBUG:
                            print(f'pos: {pos}')
                        if DEBUG:
                            print(f'param: {param}')
                        if DEBUG:
                            print(f'inner_idx: {inner_idx}')
                        if param == 0:
                            if DEBUG:
                                print(f'val posmode {idx}: {idx}')
                            vals.append(instructions[idx])
                        elif param == 1:
                            if DEBUG:
                                print(f'val immmode RAW: {idx}')
                            vals.append(idx)
                if not len(vals):
                    if DEBUG:
                        print(
                            f'plain opcode {opcode} getting val +1 at {i + 1}')
                    if opcode == 3:
                        vals.append(instructions[i + 1])
                    else:
                        vals.append(instructions[instructions[i + 1]])
                    if opcode != 3 and opcode != 4:
                        if DEBUG:
                            print(
                                f'plain opcode {opcode} getting val +2 at {i + 2}'
                                )
                        vals.append(instructions[instructions[i + 2]])
                if opcode == 5 or opcode == 6:
                    if DEBUG:
                        print(f'vals op5/6: {vals}')
                    jump = 3
                    if opcode == 5:
                        if vals[0] != 0:
                            if DEBUG:
                                print(f'JUMP TO : {vals[1]}')
                            i = vals[1]
                            continue
                        else:
                            pass
                    elif opcode == 6:
                        if vals[0] == 0:
                            if DEBUG:
                                print(f'JUMP TO : {vals[1]}')
                            i = vals[1]
                            continue
                        else:
                            pass
                elif opcode == 4:
                    jump = 2
                    if DEBUG:
                        print(f'vals op4: {vals}')
                    diag_code = vals[0]
                    test_c += 1
                elif opcode == 3:
                    jump = 2
                    if DEBUG:
                        print(f'vals op3: {vals}')
                    instructions[vals[0]] = inputt
                    if DEBUG:
                        print(f'write pos: {vals[0]}')
                    if DEBUG:
                        print(f'write val: {inputt}')
                elif opcode == 1 or opcode == 2 or opcode == 7 or opcode == 8:
                    if DEBUG:
                        print(f'vals op1/2/7/8: {vals}')
                    result = -1
                    if opcode == 1:
                        result = reduce(lambda x, y: x + y, vals)
                    elif opcode == 2:
                        result = reduce(lambda x, y: x * y, vals)
                    elif opcode == 7:
                        result = 1 if vals[0] < vals[1] else 0
                    elif opcode == 8:
                        result = 1 if vals[0] == vals[1] else 0
                    position = instructions[i + (jump - 1)]
                    instructions[position] = result
                    if DEBUG:
                        print(f'write pos: {position}')
                    if DEBUG:
                        print(f'write val: {result}')
                else:
                    raise Exception('opcode not understood: ', opcode)
            if DEBUG:
                print(f'jump: {jump}')
            i += jump
    except:
        raise Exception('Unknown exception')
    raise Exception('process_instructions finished organically')
    return False


class testCase(unittest.TestCase):

    def test_process_instructions(self):
        self.assertEqual(process_instructions([1, 9, 10, 3, 2, 3, 11, 0, 99,
            30, 40, 50])[1], [3500, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50],
            '[1,9,10,3,2,3,11,0,99,30,40,50]')
        self.assertEqual(process_instructions([1, 0, 0, 0, 99])[1], [2, 0, 
            0, 0, 99], [1, 0, 0, 0, 99])
        self.assertEqual(process_instructions([2, 3, 0, 3, 99])[1], [2, 3, 
            0, 6, 99], [2, 3, 0, 3, 99])
        self.assertEqual(process_instructions([2, 4, 4, 5, 99, 0])[1], [2, 
            4, 4, 5, 99, 9801], [2, 4, 4, 5, 99, 0])
        self.assertEqual(process_instructions([1, 1, 1, 4, 99, 5, 6, 0, 99]
            )[1], [30, 1, 1, 4, 2, 5, 6, 0, 99], [1, 1, 1, 4, 99, 5, 6, 0, 99])
        self.assertEqual(process_instructions([1002, 4, 3, 4, 33])[1], [
            1002, 4, 3, 4, 99], '[1002,4,3,4,33]')
        self.assertEqual(process_instructions([3, 0, 4, 0, 99], 34939329)[0
            ], 34939329, '[3,0,4,0,99],34939329')
        self.assertEqual(process_instructions([3, 9, 8, 9, 10, 9, 4, 9, 99,
            -1, 8], 8)[0], 1, '[3,9,8,9,10,9,4,9,99,-1,8],8')
        self.assertEqual(process_instructions([3, 9, 8, 9, 10, 9, 4, 9, 99,
            -1, 8], 3)[0], 0, '[3,9,8,9,10,9,4,9,99,-1,8],3')
        self.assertEqual(process_instructions([3, 3, 1108, -1, 8, 3, 4, 3, 
            99], 8)[0], 1, '[3,3,1108,-1,8,3,4,3,99],8')
        self.assertEqual(process_instructions([3, 3, 1108, -1, 8, 3, 4, 3, 
            99], 3)[0], 0, '[3,3,1108,-1,8,3,4,3,99],3')
        self.assertEqual(process_instructions([3, 12, 6, 12, 15, 1, 13, 14,
            13, 4, 13, 99, -1, 0, 1, 9], 0)[0], 0,
            '[3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9],0')
        self.assertEqual(process_instructions([3, 12, 6, 12, 15, 1, 13, 14,
            13, 4, 13, 99, -1, 0, 1, 9], 1291278)[0], 1,
            '[3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9],1291278')
        self.assertEqual(process_instructions([3, 3, 1105, -1, 9, 1101, 0, 
            0, 12, 4, 12, 99, 1], 0)[0], 0,
            '[3,3,1105,-1,9,1101,0,0,12,4,12,99,1],0')
        self.assertEqual(process_instructions([3, 3, 1105, -1, 9, 1101, 0, 
            0, 12, 4, 12, 99, 1], 3738281)[0], 1,
            '[3,3,1105,-1,9,1101,0,0,12,4,12,99,1],3738281')
        self.assertEqual(process_instructions([3, 21, 1008, 21, 8, 20, 1005,
            20, 22, 107, 8, 21, 20, 1006, 20, 31, 1106, 0, 36, 98, 0, 0, 
            1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104, 999, 1105, 1, 46, 
            1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99], 5)[0], 999,
            '[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],5'
            )
        self.assertEqual(process_instructions([3, 21, 1008, 21, 8, 20, 1005,
            20, 22, 107, 8, 21, 20, 1006, 20, 31, 1106, 0, 36, 98, 0, 0, 
            1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104, 999, 1105, 1, 46, 
            1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99], 8)[0], 1000,
            '[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],8'
            )
        self.assertEqual(process_instructions([3, 21, 1008, 21, 8, 20, 1005,
            20, 22, 107, 8, 21, 20, 1006, 20, 31, 1106, 0, 36, 98, 0, 0, 
            1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104, 999, 1105, 1, 46, 
            1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99], 7347289)[0], 
            1001,
            '[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],7347289'
            )


if __name__ == '__main__':
    try:
        sys.argv[1]
        puzzle_text()
    except:
        instructions = [3, 225, 1, 225, 6, 6, 1100, 1, 238, 225, 104, 0, 
            1001, 191, 50, 224, 101, -64, 224, 224, 4, 224, 1002, 223, 8, 
            223, 101, 5, 224, 224, 1, 224, 223, 223, 2, 150, 218, 224, 1001,
            224, -1537, 224, 4, 224, 102, 8, 223, 223, 1001, 224, 2, 224, 1,
            223, 224, 223, 1002, 154, 5, 224, 101, -35, 224, 224, 4, 224, 
            1002, 223, 8, 223, 1001, 224, 5, 224, 1, 224, 223, 223, 1102, 
            76, 17, 225, 1102, 21, 44, 224, 1001, 224, -924, 224, 4, 224, 
            102, 8, 223, 223, 1001, 224, 4, 224, 1, 224, 223, 223, 101, 37,
            161, 224, 101, -70, 224, 224, 4, 224, 1002, 223, 8, 223, 101, 6,
            224, 224, 1, 223, 224, 223, 102, 46, 157, 224, 1001, 224, -1978,
            224, 4, 224, 102, 8, 223, 223, 1001, 224, 5, 224, 1, 224, 223, 
            223, 1102, 5, 29, 225, 1101, 10, 7, 225, 1101, 43, 38, 225, 
            1102, 33, 46, 225, 1, 80, 188, 224, 1001, 224, -73, 224, 4, 224,
            102, 8, 223, 223, 101, 4, 224, 224, 1, 224, 223, 223, 1101, 52,
            56, 225, 1101, 14, 22, 225, 1101, 66, 49, 224, 1001, 224, -115,
            224, 4, 224, 1002, 223, 8, 223, 1001, 224, 7, 224, 1, 224, 223,
            223, 1101, 25, 53, 225, 4, 223, 99, 0, 0, 0, 677, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 1105, 0, 99999, 1105, 227, 247, 1105, 1, 
            99999, 1005, 227, 99999, 1005, 0, 256, 1105, 1, 99999, 1106, 
            227, 99999, 1106, 0, 265, 1105, 1, 99999, 1006, 0, 99999, 1006,
            227, 274, 1105, 1, 99999, 1105, 1, 280, 1105, 1, 99999, 1, 225,
            225, 225, 1101, 294, 0, 0, 105, 1, 0, 1105, 1, 99999, 1106, 0, 
            300, 1105, 1, 99999, 1, 225, 225, 225, 1101, 314, 0, 0, 106, 0,
            0, 1105, 1, 99999, 108, 226, 226, 224, 1002, 223, 2, 223, 1005,
            224, 329, 101, 1, 223, 223, 108, 677, 677, 224, 1002, 223, 2, 
            223, 1006, 224, 344, 1001, 223, 1, 223, 8, 677, 677, 224, 102, 
            2, 223, 223, 1006, 224, 359, 101, 1, 223, 223, 7, 226, 677, 224,
            102, 2, 223, 223, 1005, 224, 374, 101, 1, 223, 223, 107, 226, 
            226, 224, 102, 2, 223, 223, 1006, 224, 389, 101, 1, 223, 223, 7,
            677, 226, 224, 1002, 223, 2, 223, 1006, 224, 404, 1001, 223, 1,
            223, 1107, 677, 226, 224, 1002, 223, 2, 223, 1006, 224, 419, 
            1001, 223, 1, 223, 1007, 226, 226, 224, 102, 2, 223, 223, 1005,
            224, 434, 101, 1, 223, 223, 1008, 226, 677, 224, 102, 2, 223, 
            223, 1005, 224, 449, 1001, 223, 1, 223, 1007, 677, 677, 224, 
            1002, 223, 2, 223, 1006, 224, 464, 1001, 223, 1, 223, 1008, 226,
            226, 224, 102, 2, 223, 223, 1006, 224, 479, 101, 1, 223, 223, 
            1007, 226, 677, 224, 1002, 223, 2, 223, 1005, 224, 494, 1001, 
            223, 1, 223, 108, 226, 677, 224, 1002, 223, 2, 223, 1006, 224, 
            509, 101, 1, 223, 223, 8, 226, 677, 224, 102, 2, 223, 223, 1005,
            224, 524, 1001, 223, 1, 223, 107, 677, 677, 224, 1002, 223, 2, 
            223, 1005, 224, 539, 101, 1, 223, 223, 107, 226, 677, 224, 1002,
            223, 2, 223, 1006, 224, 554, 101, 1, 223, 223, 1107, 226, 677, 
            224, 1002, 223, 2, 223, 1006, 224, 569, 1001, 223, 1, 223, 1108,
            677, 226, 224, 102, 2, 223, 223, 1005, 224, 584, 1001, 223, 1, 
            223, 1008, 677, 677, 224, 102, 2, 223, 223, 1005, 224, 599, 
            1001, 223, 1, 223, 1107, 677, 677, 224, 102, 2, 223, 223, 1006,
            224, 614, 101, 1, 223, 223, 7, 226, 226, 224, 102, 2, 223, 223,
            1005, 224, 629, 1001, 223, 1, 223, 1108, 677, 677, 224, 102, 2,
            223, 223, 1006, 224, 644, 1001, 223, 1, 223, 8, 677, 226, 224, 
            1002, 223, 2, 223, 1005, 224, 659, 101, 1, 223, 223, 1108, 226,
            677, 224, 102, 2, 223, 223, 1005, 224, 674, 101, 1, 223, 223, 4,
            223, 99, 226]
        f = process_instructions(instructions.copy(), 1)
        print(f[0])
        print()
        f = process_instructions(instructions.copy(), 5)
        print(f[0])
    print('done')


def puzzle_text():
    print(
        """
--- Day 5: Sunny with a Chance of Asteroids ---
You're starting to sweat as the ship makes its way toward Mercury. The Elves suggest that you get the air conditioner working by upgrading your ship computer to support the Thermal Environment Supervision Terminal.

The Thermal Environment Supervision Terminal (TEST) starts by running a diagnostic program (your puzzle input). The TEST diagnostic program will run on your existing Intcode computer after a few modifications:

First, you'll need to add two new instructions:

Opcode 3 takes a single integer as input and saves it to the position given by its only parameter. For example, the instruction 3,50 would take an input value and store it at address 50.
Opcode 4 outputs the value of its only parameter. For example, the instruction 4,50 would output the value at address 50.
Programs that use these instructions will come with documentation that explains what should be connected to the input and output. The program 3,0,4,0,99 outputs whatever it gets as input, then halts.

Second, you'll need to add support for parameter modes:

Each parameter of an instruction is handled based on its parameter mode. Right now, your ship computer already understands parameter mode 0, position mode, which causes the parameter to be interpreted as a position - if the parameter is 50, its value is the value stored at address 50 in memory. Until now, all parameters have been in position mode.

Now, your ship computer will also need to handle parameters in mode 1, immediate mode. In immediate mode, a parameter is interpreted as a value - if the parameter is 50, its value is simply 50.

Parameter modes are stored in the same value as the instruction's opcode. The opcode is a two-digit number based only on the ones and tens digit of the value, that is, the opcode is the rightmost two digits of the first value in an instruction. Parameter modes are single digits, one per parameter, read right-to-left from the opcode: the first parameter's mode is in the hundreds digit, the second parameter's mode is in the thousands digit, the third parameter's mode is in the ten-thousands digit, and so on. Any missing modes are 0.

For example, consider the program 1002,4,3,4,33.

The first instruction, 1002,4,3,4, is a multiply instruction - the rightmost two digits of the first value, 02, indicate opcode 2, multiplication. Then, going right to left, the parameter modes are 0 (hundreds digit), 1 (thousands digit), and 0 (ten-thousands digit, not present and therefore zero):

ABCDE
 1002

DE - two-digit opcode,      02 == opcode 2
 C - mode of 1st parameter,  0 == position mode
 B - mode of 2nd parameter,  1 == immediate mode
 A - mode of 3rd parameter,  0 == position mode,
                                  omitted due to being a leading zero
This instruction multiplies its first two parameters. The first parameter, 4 in position mode, works like it did before - its value is the value stored at address 4 (33). The second parameter, 3 in immediate mode, simply has value 3. The result of this operation, 33 * 3 = 99, is written according to the third parameter, 4 in position mode, which also works like it did before - 99 is written to address 4.

Parameters that an instruction writes to will never be in immediate mode.

Finally, some notes:

It is important to remember that the instruction pointer should increase by the number of values in the instruction after the instruction finishes. Because of the new instructions, this amount is no longer always 4.
Integers can be negative: 1101,100,-1,4,0 is a valid program (find 100 + -1, store the result in position 4).
The TEST diagnostic program will start by requesting from the user the ID of the system to test by running an input instruction - provide it 1, the ID for the ship's air conditioner unit.

It will then perform a series of diagnostic tests confirming that various parts of the Intcode computer, like parameter modes, function correctly. For each test, it will run an output instruction indicating how far the result of the test was from the expected value, where 0 means the test was successful. Non-zero outputs mean that a function is not working correctly; check the instructions that were run before the output instruction to see which one failed.

Finally, the program will output a diagnostic code and immediately halt. This final output isn't an error; an output followed immediately by a halt means the program finished. If all outputs were zero except the diagnostic code, the diagnostic program ran successfully.

After providing 1 to the only input instruction and passing all the tests, what diagnostic code does the program produce?

Your puzzle answer was 11193703.

--- Part Two ---
The air conditioner comes online! Its cold air feels good for a while, but then the TEST alarms start to go off. Since the air conditioner can't vent its heat anywhere but back into the spacecraft, it's actually making the air inside the ship warmer.

Instead, you'll need to use the TEST to extend the thermal radiators. Fortunately, the diagnostic program (your puzzle input) is already equipped for this. Unfortunately, your Intcode computer is not.

Your computer is only missing a few opcodes:

Opcode 5 is jump-if-true: if the first parameter is non-zero, it sets the instruction pointer to the value from the second parameter. Otherwise, it does nothing.
Opcode 6 is jump-if-false: if the first parameter is zero, it sets the instruction pointer to the value from the second parameter. Otherwise, it does nothing.
Opcode 7 is less than: if the first parameter is less than the second parameter, it stores 1 in the position given by the third parameter. Otherwise, it stores 0.
Opcode 8 is equals: if the first parameter is equal to the second parameter, it stores 1 in the position given by the third parameter. Otherwise, it stores 0.
Like all instructions, these instructions need to support parameter modes as described above.

Normally, after an instruction is finished, the instruction pointer increases by the number of values in that instruction. However, if the instruction modifies the instruction pointer, that value is used and the instruction pointer is not automatically increased.

For example, here are several programs that take one input, compare it to the value 8, and then produce one output:

3,9,8,9,10,9,4,9,99,-1,8 - Using position mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
3,9,7,9,10,9,4,9,99,-1,8 - Using position mode, consider whether the input is less than 8; output 1 (if it is) or 0 (if it is not).
3,3,1108,-1,8,3,4,3,99 - Using immediate mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
3,3,1107,-1,8,3,4,3,99 - Using immediate mode, consider whether the input is less than 8; output 1 (if it is) or 0 (if it is not).
Here are some jump tests that take an input, then output 0 if the input was zero or 1 if the input was non-zero:

3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9 (using position mode)
3,3,1105,-1,9,1101,0,0,12,4,12,99,1 (using immediate mode)
Here's a larger example:

3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,
1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,
999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99
The above example program uses an input instruction to ask for a single number. The program will then output 999 if the input value is below 8, output 1000 if the input value is equal to 8, or output 1001 if the input value is greater than 8.

This time, when the TEST diagnostic program runs its input instruction to get the ID of the system to test, provide it 5, the ID for the ship's thermal radiator controller. This diagnostic test suite only outputs one number, the diagnostic code.

What is the diagnostic code for system ID 5?

Your puzzle answer was 12410607.

Both parts of this puzzle are complete! They provide two gold stars: **
"""
        )

import sys
import unittest
from functools import reduce
DEBUG = False


def process_instructions(instructions, inputt=-1):
    i = 0
    test_c = 1
    diag_code = -1
    num_instrux = len(instructions)
    if DEBUG:
        print(f'instructions: {instructions}')
    if DEBUG:
        print(f'inputt: {inputt}')
    try:
        jump = 999999
        while i < num_instrux:
            if DEBUG:
                print(f'\n{i}: {instructions[i:i + 20]}...')
            opcode = instructions[i]
            if DEBUG:
                print(f'opcode: {opcode}')
            if opcode == 99:
                jump = 0
                if DEBUG:
                    print(' ___ 99 ___ \n\n')
                return diag_code, instructions
            else:
                vals = []
                jump = 4
                if opcode > 99:
                    s_opcode = str(opcode)
                    jump = len(s_opcode)
                    if jump == 3 and opcode != 104 and opcode != 103:
                        s_opcode = f'0{s_opcode}'
                        jump = len(s_opcode)
                    num_params = jump - 2
                    opcode = int(s_opcode[-2:])
                    if not 9 > opcode > 0:
                        raise Exception('bad opcode encoded: {s_opcode}')
                    if DEBUG:
                        print(f'opcode: {opcode}')
                    if DEBUG:
                        print(f's_opcode: {s_opcode}')
                    if DEBUG:
                        print(f's_opcode[-2:]: {s_opcode[-2:]}')
                    if DEBUG:
                        print(f'processed jump: {jump}')
                    for d in range(0, num_params):
                        pos = -2 - (d + 1)
                        param = int(s_opcode[pos])
                        inner_idx = i + d + 1
                        idx = instructions[inner_idx]
                        if DEBUG:
                            print(f'pos: {pos}')
                        if DEBUG:
                            print(f'param: {param}')
                        if DEBUG:
                            print(f'inner_idx: {inner_idx}')
                        if param == 0:
                            if DEBUG:
                                print(f'val posmode {idx}: {idx}')
                            vals.append(instructions[idx])
                        elif param == 1:
                            if DEBUG:
                                print(f'val immmode RAW: {idx}')
                            vals.append(idx)
                if not len(vals):
                    if DEBUG:
                        print(
                            f'plain opcode {opcode} getting val +1 at {i + 1}')
                    if opcode == 3:
                        vals.append(instructions[i + 1])
                    else:
                        vals.append(instructions[instructions[i + 1]])
                    if opcode != 3 and opcode != 4:
                        if DEBUG:
                            print(
                                f'plain opcode {opcode} getting val +2 at {i + 2}'
                                )
                        vals.append(instructions[instructions[i + 2]])
                if opcode == 5 or opcode == 6:
                    if DEBUG:
                        print(f'vals op5/6: {vals}')
                    jump = 3
                    if opcode == 5:
                        if vals[0] != 0:
                            if DEBUG:
                                print(f'JUMP TO : {vals[1]}')
                            i = vals[1]
                            continue
                        else:
                            pass
                    elif opcode == 6:
                        if vals[0] == 0:
                            if DEBUG:
                                print(f'JUMP TO : {vals[1]}')
                            i = vals[1]
                            continue
                        else:
                            pass
                elif opcode == 4:
                    jump = 2
                    if DEBUG:
                        print(f'vals op4: {vals}')
                    diag_code = vals[0]
                    test_c += 1
                elif opcode == 3:
                    jump = 2
                    if DEBUG:
                        print(f'vals op3: {vals}')
                    instructions[vals[0]] = inputt
                    if DEBUG:
                        print(f'write pos: {vals[0]}')
                    if DEBUG:
                        print(f'write val: {inputt}')
                elif opcode == 1 or opcode == 2 or opcode == 7 or opcode == 8:
                    if DEBUG:
                        print(f'vals op1/2/7/8: {vals}')
                    result = -1
                    if opcode == 1:
                        result = reduce(lambda x, y: x + y, vals)
                    elif opcode == 2:
                        result = reduce(lambda x, y: x * y, vals)
                    elif opcode == 7:
                        result = 1 if vals[0] < vals[1] else 0
                    elif opcode == 8:
                        result = 1 if vals[0] == vals[1] else 0
                    position = instructions[i + (jump - 1)]
                    instructions[position] = result
                    if DEBUG:
                        print(f'write pos: {position}')
                    if DEBUG:
                        print(f'write val: {result}')
                else:
                    raise Exception('opcode not understood: ', opcode)
            if DEBUG:
                print(f'jump: {jump}')
            i += jump
    except:
        raise Exception('Unknown exception')
    raise Exception('process_instructions finished organically')
    return False


class testCase(unittest.TestCase):

    def test_process_instructions(self):
        self.assertEqual(process_instructions([1, 9, 10, 3, 2, 3, 11, 0, 99,
            30, 40, 50])[1], [3500, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50],
            '[1,9,10,3,2,3,11,0,99,30,40,50]')
        self.assertEqual(process_instructions([1, 0, 0, 0, 99])[1], [2, 0, 
            0, 0, 99], [1, 0, 0, 0, 99])
        self.assertEqual(process_instructions([2, 3, 0, 3, 99])[1], [2, 3, 
            0, 6, 99], [2, 3, 0, 3, 99])
        self.assertEqual(process_instructions([2, 4, 4, 5, 99, 0])[1], [2, 
            4, 4, 5, 99, 9801], [2, 4, 4, 5, 99, 0])
        self.assertEqual(process_instructions([1, 1, 1, 4, 99, 5, 6, 0, 99]
            )[1], [30, 1, 1, 4, 2, 5, 6, 0, 99], [1, 1, 1, 4, 99, 5, 6, 0, 99])
        self.assertEqual(process_instructions([1002, 4, 3, 4, 33])[1], [
            1002, 4, 3, 4, 99], '[1002,4,3,4,33]')
        self.assertEqual(process_instructions([3, 0, 4, 0, 99], 34939329)[0
            ], 34939329, '[3,0,4,0,99],34939329')
        self.assertEqual(process_instructions([3, 9, 8, 9, 10, 9, 4, 9, 99,
            -1, 8], 8)[0], 1, '[3,9,8,9,10,9,4,9,99,-1,8],8')
        self.assertEqual(process_instructions([3, 9, 8, 9, 10, 9, 4, 9, 99,
            -1, 8], 3)[0], 0, '[3,9,8,9,10,9,4,9,99,-1,8],3')
        self.assertEqual(process_instructions([3, 3, 1108, -1, 8, 3, 4, 3, 
            99], 8)[0], 1, '[3,3,1108,-1,8,3,4,3,99],8')
        self.assertEqual(process_instructions([3, 3, 1108, -1, 8, 3, 4, 3, 
            99], 3)[0], 0, '[3,3,1108,-1,8,3,4,3,99],3')
        self.assertEqual(process_instructions([3, 12, 6, 12, 15, 1, 13, 14,
            13, 4, 13, 99, -1, 0, 1, 9], 0)[0], 0,
            '[3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9],0')
        self.assertEqual(process_instructions([3, 12, 6, 12, 15, 1, 13, 14,
            13, 4, 13, 99, -1, 0, 1, 9], 1291278)[0], 1,
            '[3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9],1291278')
        self.assertEqual(process_instructions([3, 3, 1105, -1, 9, 1101, 0, 
            0, 12, 4, 12, 99, 1], 0)[0], 0,
            '[3,3,1105,-1,9,1101,0,0,12,4,12,99,1],0')
        self.assertEqual(process_instructions([3, 3, 1105, -1, 9, 1101, 0, 
            0, 12, 4, 12, 99, 1], 3738281)[0], 1,
            '[3,3,1105,-1,9,1101,0,0,12,4,12,99,1],3738281')
        self.assertEqual(process_instructions([3, 21, 1008, 21, 8, 20, 1005,
            20, 22, 107, 8, 21, 20, 1006, 20, 31, 1106, 0, 36, 98, 0, 0, 
            1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104, 999, 1105, 1, 46, 
            1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99], 5)[0], 999,
            '[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],5'
            )
        self.assertEqual(process_instructions([3, 21, 1008, 21, 8, 20, 1005,
            20, 22, 107, 8, 21, 20, 1006, 20, 31, 1106, 0, 36, 98, 0, 0, 
            1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104, 999, 1105, 1, 46, 
            1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99], 8)[0], 1000,
            '[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],8'
            )
        self.assertEqual(process_instructions([3, 21, 1008, 21, 8, 20, 1005,
            20, 22, 107, 8, 21, 20, 1006, 20, 31, 1106, 0, 36, 98, 0, 0, 
            1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104, 999, 1105, 1, 46, 
            1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99], 7347289)[0], 
            1001,
            '[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],7347289'
            )


if __name__ == '__main__':
    try:
        sys.argv[1]
        puzzle_text()
    except:
        instructions = [3, 225, 1, 225, 6, 6, 1100, 1, 238, 225, 104, 0, 
            1001, 191, 50, 224, 101, -64, 224, 224, 4, 224, 1002, 223, 8, 
            223, 101, 5, 224, 224, 1, 224, 223, 223, 2, 150, 218, 224, 1001,
            224, -1537, 224, 4, 224, 102, 8, 223, 223, 1001, 224, 2, 224, 1,
            223, 224, 223, 1002, 154, 5, 224, 101, -35, 224, 224, 4, 224, 
            1002, 223, 8, 223, 1001, 224, 5, 224, 1, 224, 223, 223, 1102, 
            76, 17, 225, 1102, 21, 44, 224, 1001, 224, -924, 224, 4, 224, 
            102, 8, 223, 223, 1001, 224, 4, 224, 1, 224, 223, 223, 101, 37,
            161, 224, 101, -70, 224, 224, 4, 224, 1002, 223, 8, 223, 101, 6,
            224, 224, 1, 223, 224, 223, 102, 46, 157, 224, 1001, 224, -1978,
            224, 4, 224, 102, 8, 223, 223, 1001, 224, 5, 224, 1, 224, 223, 
            223, 1102, 5, 29, 225, 1101, 10, 7, 225, 1101, 43, 38, 225, 
            1102, 33, 46, 225, 1, 80, 188, 224, 1001, 224, -73, 224, 4, 224,
            102, 8, 223, 223, 101, 4, 224, 224, 1, 224, 223, 223, 1101, 52,
            56, 225, 1101, 14, 22, 225, 1101, 66, 49, 224, 1001, 224, -115,
            224, 4, 224, 1002, 223, 8, 223, 1001, 224, 7, 224, 1, 224, 223,
            223, 1101, 25, 53, 225, 4, 223, 99, 0, 0, 0, 677, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 1105, 0, 99999, 1105, 227, 247, 1105, 1, 
            99999, 1005, 227, 99999, 1005, 0, 256, 1105, 1, 99999, 1106, 
            227, 99999, 1106, 0, 265, 1105, 1, 99999, 1006, 0, 99999, 1006,
            227, 274, 1105, 1, 99999, 1105, 1, 280, 1105, 1, 99999, 1, 225,
            225, 225, 1101, 294, 0, 0, 105, 1, 0, 1105, 1, 99999, 1106, 0, 
            300, 1105, 1, 99999, 1, 225, 225, 225, 1101, 314, 0, 0, 106, 0,
            0, 1105, 1, 99999, 108, 226, 226, 224, 1002, 223, 2, 223, 1005,
            224, 329, 101, 1, 223, 223, 108, 677, 677, 224, 1002, 223, 2, 
            223, 1006, 224, 344, 1001, 223, 1, 223, 8, 677, 677, 224, 102, 
            2, 223, 223, 1006, 224, 359, 101, 1, 223, 223, 7, 226, 677, 224,
            102, 2, 223, 223, 1005, 224, 374, 101, 1, 223, 223, 107, 226, 
            226, 224, 102, 2, 223, 223, 1006, 224, 389, 101, 1, 223, 223, 7,
            677, 226, 224, 1002, 223, 2, 223, 1006, 224, 404, 1001, 223, 1,
            223, 1107, 677, 226, 224, 1002, 223, 2, 223, 1006, 224, 419, 
            1001, 223, 1, 223, 1007, 226, 226, 224, 102, 2, 223, 223, 1005,
            224, 434, 101, 1, 223, 223, 1008, 226, 677, 224, 102, 2, 223, 
            223, 1005, 224, 449, 1001, 223, 1, 223, 1007, 677, 677, 224, 
            1002, 223, 2, 223, 1006, 224, 464, 1001, 223, 1, 223, 1008, 226,
            226, 224, 102, 2, 223, 223, 1006, 224, 479, 101, 1, 223, 223, 
            1007, 226, 677, 224, 1002, 223, 2, 223, 1005, 224, 494, 1001, 
            223, 1, 223, 108, 226, 677, 224, 1002, 223, 2, 223, 1006, 224, 
            509, 101, 1, 223, 223, 8, 226, 677, 224, 102, 2, 223, 223, 1005,
            224, 524, 1001, 223, 1, 223, 107, 677, 677, 224, 1002, 223, 2, 
            223, 1005, 224, 539, 101, 1, 223, 223, 107, 226, 677, 224, 1002,
            223, 2, 223, 1006, 224, 554, 101, 1, 223, 223, 1107, 226, 677, 
            224, 1002, 223, 2, 223, 1006, 224, 569, 1001, 223, 1, 223, 1108,
            677, 226, 224, 102, 2, 223, 223, 1005, 224, 584, 1001, 223, 1, 
            223, 1008, 677, 677, 224, 102, 2, 223, 223, 1005, 224, 599, 
            1001, 223, 1, 223, 1107, 677, 677, 224, 102, 2, 223, 223, 1006,
            224, 614, 101, 1, 223, 223, 7, 226, 226, 224, 102, 2, 223, 223,
            1005, 224, 629, 1001, 223, 1, 223, 1108, 677, 677, 224, 102, 2,
            223, 223, 1006, 224, 644, 1001, 223, 1, 223, 8, 677, 226, 224, 
            1002, 223, 2, 223, 1005, 224, 659, 101, 1, 223, 223, 1108, 226,
            677, 224, 102, 2, 223, 223, 1005, 224, 674, 101, 1, 223, 223, 4,
            223, 99, 226]
        f = process_instructions(instructions.copy(), 1)
        print(f[0])
        print()
        f = process_instructions(instructions.copy(), 5)
        print(f[0])
    print('done')


def puzzle_text():
    print(
        """
--- Day 5: Sunny with a Chance of Asteroids ---
You're starting to sweat as the ship makes its way toward Mercury. The Elves suggest that you get the air conditioner working by upgrading your ship computer to support the Thermal Environment Supervision Terminal.

The Thermal Environment Supervision Terminal (TEST) starts by running a diagnostic program (your puzzle input). The TEST diagnostic program will run on your existing Intcode computer after a few modifications:

First, you'll need to add two new instructions:

Opcode 3 takes a single integer as input and saves it to the position given by its only parameter. For example, the instruction 3,50 would take an input value and store it at address 50.
Opcode 4 outputs the value of its only parameter. For example, the instruction 4,50 would output the value at address 50.
Programs that use these instructions will come with documentation that explains what should be connected to the input and output. The program 3,0,4,0,99 outputs whatever it gets as input, then halts.

Second, you'll need to add support for parameter modes:

Each parameter of an instruction is handled based on its parameter mode. Right now, your ship computer already understands parameter mode 0, position mode, which causes the parameter to be interpreted as a position - if the parameter is 50, its value is the value stored at address 50 in memory. Until now, all parameters have been in position mode.

Now, your ship computer will also need to handle parameters in mode 1, immediate mode. In immediate mode, a parameter is interpreted as a value - if the parameter is 50, its value is simply 50.

Parameter modes are stored in the same value as the instruction's opcode. The opcode is a two-digit number based only on the ones and tens digit of the value, that is, the opcode is the rightmost two digits of the first value in an instruction. Parameter modes are single digits, one per parameter, read right-to-left from the opcode: the first parameter's mode is in the hundreds digit, the second parameter's mode is in the thousands digit, the third parameter's mode is in the ten-thousands digit, and so on. Any missing modes are 0.

For example, consider the program 1002,4,3,4,33.

The first instruction, 1002,4,3,4, is a multiply instruction - the rightmost two digits of the first value, 02, indicate opcode 2, multiplication. Then, going right to left, the parameter modes are 0 (hundreds digit), 1 (thousands digit), and 0 (ten-thousands digit, not present and therefore zero):

ABCDE
 1002

DE - two-digit opcode,      02 == opcode 2
 C - mode of 1st parameter,  0 == position mode
 B - mode of 2nd parameter,  1 == immediate mode
 A - mode of 3rd parameter,  0 == position mode,
                                  omitted due to being a leading zero
This instruction multiplies its first two parameters. The first parameter, 4 in position mode, works like it did before - its value is the value stored at address 4 (33). The second parameter, 3 in immediate mode, simply has value 3. The result of this operation, 33 * 3 = 99, is written according to the third parameter, 4 in position mode, which also works like it did before - 99 is written to address 4.

Parameters that an instruction writes to will never be in immediate mode.

Finally, some notes:

It is important to remember that the instruction pointer should increase by the number of values in the instruction after the instruction finishes. Because of the new instructions, this amount is no longer always 4.
Integers can be negative: 1101,100,-1,4,0 is a valid program (find 100 + -1, store the result in position 4).
The TEST diagnostic program will start by requesting from the user the ID of the system to test by running an input instruction - provide it 1, the ID for the ship's air conditioner unit.

It will then perform a series of diagnostic tests confirming that various parts of the Intcode computer, like parameter modes, function correctly. For each test, it will run an output instruction indicating how far the result of the test was from the expected value, where 0 means the test was successful. Non-zero outputs mean that a function is not working correctly; check the instructions that were run before the output instruction to see which one failed.

Finally, the program will output a diagnostic code and immediately halt. This final output isn't an error; an output followed immediately by a halt means the program finished. If all outputs were zero except the diagnostic code, the diagnostic program ran successfully.

After providing 1 to the only input instruction and passing all the tests, what diagnostic code does the program produce?

Your puzzle answer was 11193703.

--- Part Two ---
The air conditioner comes online! Its cold air feels good for a while, but then the TEST alarms start to go off. Since the air conditioner can't vent its heat anywhere but back into the spacecraft, it's actually making the air inside the ship warmer.

Instead, you'll need to use the TEST to extend the thermal radiators. Fortunately, the diagnostic program (your puzzle input) is already equipped for this. Unfortunately, your Intcode computer is not.

Your computer is only missing a few opcodes:

Opcode 5 is jump-if-true: if the first parameter is non-zero, it sets the instruction pointer to the value from the second parameter. Otherwise, it does nothing.
Opcode 6 is jump-if-false: if the first parameter is zero, it sets the instruction pointer to the value from the second parameter. Otherwise, it does nothing.
Opcode 7 is less than: if the first parameter is less than the second parameter, it stores 1 in the position given by the third parameter. Otherwise, it stores 0.
Opcode 8 is equals: if the first parameter is equal to the second parameter, it stores 1 in the position given by the third parameter. Otherwise, it stores 0.
Like all instructions, these instructions need to support parameter modes as described above.

Normally, after an instruction is finished, the instruction pointer increases by the number of values in that instruction. However, if the instruction modifies the instruction pointer, that value is used and the instruction pointer is not automatically increased.

For example, here are several programs that take one input, compare it to the value 8, and then produce one output:

3,9,8,9,10,9,4,9,99,-1,8 - Using position mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
3,9,7,9,10,9,4,9,99,-1,8 - Using position mode, consider whether the input is less than 8; output 1 (if it is) or 0 (if it is not).
3,3,1108,-1,8,3,4,3,99 - Using immediate mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
3,3,1107,-1,8,3,4,3,99 - Using immediate mode, consider whether the input is less than 8; output 1 (if it is) or 0 (if it is not).
Here are some jump tests that take an input, then output 0 if the input was zero or 1 if the input was non-zero:

3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9 (using position mode)
3,3,1105,-1,9,1101,0,0,12,4,12,99,1 (using immediate mode)
Here's a larger example:

3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,
1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,
999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99
The above example program uses an input instruction to ask for a single number. The program will then output 999 if the input value is below 8, output 1000 if the input value is equal to 8, or output 1001 if the input value is greater than 8.

This time, when the TEST diagnostic program runs its input instruction to get the ID of the system to test, provide it 5, the ID for the ship's thermal radiator controller. This diagnostic test suite only outputs one number, the diagnostic code.

What is the diagnostic code for system ID 5?

Your puzzle answer was 12410607.

Both parts of this puzzle are complete! They provide two gold stars: **
"""
        )

# adventofcode 2019
# crushallhumans
# puzzle 5 - intcode part 2
# 12/5/2019

import sys
import unittest
from functools import reduce 

#DEBUG = True
DEBUG = False

def process_instructions(instructions, inputt = -1):
	i = 0
	test_c = 1
	diag_code = -1
	num_instrux = len(instructions)

	if DEBUG: print (f"instructions: {instructions}")
	if DEBUG: print (f"inputt: {inputt}")

	try:
		jump = 999999
		while i < num_instrux:
			if DEBUG: print (f"\n{i}: {instructions[i:i+20]}...")

			opcode = instructions[i]
			if DEBUG: print (f"opcode: {opcode}")

			# handle exit
			if (opcode == 99):
				jump = 0
				if DEBUG: print (" ___ 99 ___ \n\n")
				return (diag_code,instructions)

			# handle opcode
			else:
				vals = []
				jump = 4

				# handle parameterized opcode
				# get operational values
				# set cursor jump value
				if (opcode > 99):
					s_opcode = str(opcode)
					jump = len(s_opcode)

					# handle leading zeroes for non-IO opcodes
					if jump == 3 and opcode != 104 and opcode != 103:
						s_opcode = f"0{s_opcode}"
						jump = len(s_opcode)

					num_params = jump - 2
					opcode = int(s_opcode[-2:])

					if not (9 > opcode > 0):
						raise Exception('bad opcode encoded: {s_opcode}')

					if DEBUG: print (f"opcode: {opcode}")
					if DEBUG: print (f"s_opcode: {s_opcode}")
					if DEBUG: print (f"s_opcode[-2:]: {s_opcode[-2:]}")
					if DEBUG: print (f"processed jump: {jump}")

					# process parameters starting from left of 0N
					for d in range(0,num_params):
						pos = -2-(d+1)
						param = int(s_opcode[pos])
						inner_idx = i+d+1
						idx = instructions[inner_idx]

						if DEBUG: print (f"pos: {pos}")
						if DEBUG: print (f"param: {param}")
						if DEBUG: print (f"inner_idx: {inner_idx}")

						# position mode
						if param == 0:
							if DEBUG: print (f"val posmode {idx}: {idx}")
							vals.append(instructions[idx])
						# immediate mode
						elif param == 1:
							if DEBUG: print (f"val immmode RAW: {idx}")
							vals.append(idx)


				# handle unparameterized opcode value collection
				if not len(vals):
					if DEBUG: print (f"plain opcode {opcode} getting val +1 at {i+1}")

					# input code is always immediate
					if opcode == 3:
						vals.append(instructions[i+1])
					else:
						vals.append(instructions[instructions[i+1]])

					# non-IO codes get a second value
					if (opcode != 3 and opcode != 4):
						if DEBUG: print (f"plain opcode {opcode} getting val +2 at {i+2}")
						vals.append(instructions[instructions[i+2]])


				#PROCESS OPCODES
				# jump codes
				if (opcode == 5 or opcode == 6):
					if DEBUG: print (f"vals op5/6: {vals}")
					jump = 3
					if (opcode == 5):
						if vals[0] != 0:
							if DEBUG: print (f"JUMP TO : {vals[1]}")
							i = vals[1]
							continue
						else:
							pass
					elif (opcode == 6):
						if vals[0] == 0:
							if DEBUG: print (f"JUMP TO : {vals[1]}")
							i = vals[1]
							continue
						else:
							pass

				# output code
				elif (opcode == 4):
					jump = 2
					if DEBUG: print (f"vals op4: {vals}")
					diag_code = vals[0]
					test_c += 1

				# input code
				elif (opcode == 3):
					jump = 2
					if DEBUG: print (f"vals op3: {vals}")
					instructions[vals[0]] = inputt
					if DEBUG: print (f"write pos: {vals[0]}")
					if DEBUG: print (f"write val: {inputt}")
						
				# operation codes
				elif (opcode == 1 or opcode == 2 or opcode == 7 or opcode == 8):
					if DEBUG: print (f"vals op1/2/7/8: {vals}")
					result = -1
					if (opcode == 1):
						result = reduce((lambda x, y: x + y), vals)
					elif (opcode == 2):
						result = reduce((lambda x, y: x * y), vals)
					elif (opcode == 7):
						result = 1 if vals[0] < vals[1] else 0
					elif (opcode == 8):
						result = 1 if vals[0] == vals[1] else 0

					position = instructions[i+(jump-1)]
					instructions[position] = result

					if DEBUG: print (f"write pos: {position}")
					if DEBUG: print (f"write val: {result}")
				else:
					raise Exception('opcode not understood: ',opcode) 
			if DEBUG: print (f"jump: {jump}")
			i += jump
	except:
		raise Exception("Unknown exception")
	raise Exception("process_instructions finished organically")
	return False


class testCase(unittest.TestCase):
	def test_process_instructions(self):
		self.assertEqual(
			process_instructions([1,9,10,3,2,3,11,0,99,30,40,50])[1],
			[3500,9,10,70,2,3,11,0,99,30,40,50],
			"[1,9,10,3,2,3,11,0,99,30,40,50]"
		)
		self.assertEqual(process_instructions([1,0,0,0,99])[1], [2,0,0,0,99], [1,0,0,0,99])
		self.assertEqual(process_instructions([2,3,0,3,99])[1], [2,3,0,6,99], [2,3,0,3,99])
		self.assertEqual(process_instructions([2,4,4,5,99,0])[1], [2,4,4,5,99,9801], [2,4,4,5,99,0])
		self.assertEqual(process_instructions([1,1,1,4,99,5,6,0,99])[1], [30,1,1,4,2,5,6,0,99], [1,1,1,4,99,5,6,0,99])

		self.assertEqual(
			process_instructions([1002,4,3,4,33])[1], 
			[1002,4,3,4,99],
			"[1002,4,3,4,33]"
		)

		self.assertEqual(
			process_instructions([3,0,4,0,99],34939329)[0],
			34939329,
			"[3,0,4,0,99],34939329"
		)

		self.assertEqual(
			process_instructions(
				[3,9,8,9,10,9,4,9,99,-1,8],
				8
			)[0],
			1,
			"[3,9,8,9,10,9,4,9,99,-1,8],8"
		)

		self.assertEqual(
			process_instructions(
				[3,9,8,9,10,9,4,9,99,-1,8],
				3
			)[0],
			0,
			"[3,9,8,9,10,9,4,9,99,-1,8],3"
		)

		self.assertEqual(
			process_instructions(
				[3,3,1108,-1,8,3,4,3,99],
				8
			)[0],
			1,
			"[3,3,1108,-1,8,3,4,3,99],8"
		)

		self.assertEqual(
			process_instructions(
				[3,3,1108,-1,8,3,4,3,99],
				3
			)[0],
			0,
			"[3,3,1108,-1,8,3,4,3,99],3"
		)


		self.assertEqual(
			process_instructions(
				[3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9],
				0
			)[0],
			0,
			"[3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9],0"
		)
		self.assertEqual(
			process_instructions(
				[3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9],
				1291278
			)[0],
			1,
			"[3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9],1291278"
		)
		self.assertEqual(
			process_instructions(
				[3,3,1105,-1,9,1101,0,0,12,4,12,99,1],
				0
			)[0],
			0,
			"[3,3,1105,-1,9,1101,0,0,12,4,12,99,1],0"
		)
		self.assertEqual(
			process_instructions(
				[3,3,1105,-1,9,1101,0,0,12,4,12,99,1],
				3738281
			)[0],
			1,
			"[3,3,1105,-1,9,1101,0,0,12,4,12,99,1],3738281"
		)

		self.assertEqual(
			process_instructions(
				[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],
				5
			)[0],
			999,
			"[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],5"
		)
		self.assertEqual(
			process_instructions(
				[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],
				8
			)[0],
			1000,
			"[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],8"
		)
		self.assertEqual(
			process_instructions(
				[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],
				7347289
			)[0],
			1001,
			"[3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99],7347289"
		)





if __name__ == '__main__':
	try:
		sys.argv[1]
		puzzle_text()

	except:
		instructions = [3,225,1,225,6,6,1100,1,238,225,104,0,1001,191,50,224,101,-64,224,224,4,224,1002,223,8,223,101,5,224,224,1,224,223,223,2,150,218,224,1001,224,-1537,224,4,224,102,8,223,223,1001,224,2,224,1,223,224,223,1002,154,5,224,101,-35,224,224,4,224,1002,223,8,223,1001,224,5,224,1,224,223,223,1102,76,17,225,1102,21,44,224,1001,224,-924,224,4,224,102,8,223,223,1001,224,4,224,1,224,223,223,101,37,161,224,101,-70,224,224,4,224,1002,223,8,223,101,6,224,224,1,223,224,223,102,46,157,224,1001,224,-1978,224,4,224,102,8,223,223,1001,224,5,224,1,224,223,223,1102,5,29,225,1101,10,7,225,1101,43,38,225,1102,33,46,225,1,80,188,224,1001,224,-73,224,4,224,102,8,223,223,101,4,224,224,1,224,223,223,1101,52,56,225,1101,14,22,225,1101,66,49,224,1001,224,-115,224,4,224,1002,223,8,223,1001,224,7,224,1,224,223,223,1101,25,53,225,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,108,226,226,224,1002,223,2,223,1005,224,329,101,1,223,223,108,677,677,224,1002,223,2,223,1006,224,344,1001,223,1,223,8,677,677,224,102,2,223,223,1006,224,359,101,1,223,223,7,226,677,224,102,2,223,223,1005,224,374,101,1,223,223,107,226,226,224,102,2,223,223,1006,224,389,101,1,223,223,7,677,226,224,1002,223,2,223,1006,224,404,1001,223,1,223,1107,677,226,224,1002,223,2,223,1006,224,419,1001,223,1,223,1007,226,226,224,102,2,223,223,1005,224,434,101,1,223,223,1008,226,677,224,102,2,223,223,1005,224,449,1001,223,1,223,1007,677,677,224,1002,223,2,223,1006,224,464,1001,223,1,223,1008,226,226,224,102,2,223,223,1006,224,479,101,1,223,223,1007,226,677,224,1002,223,2,223,1005,224,494,1001,223,1,223,108,226,677,224,1002,223,2,223,1006,224,509,101,1,223,223,8,226,677,224,102,2,223,223,1005,224,524,1001,223,1,223,107,677,677,224,1002,223,2,223,1005,224,539,101,1,223,223,107,226,677,224,1002,223,2,223,1006,224,554,101,1,223,223,1107,226,677,224,1002,223,2,223,1006,224,569,1001,223,1,223,1108,677,226,224,102,2,223,223,1005,224,584,1001,223,1,223,1008,677,677,224,102,2,223,223,1005,224,599,1001,223,1,223,1107,677,677,224,102,2,223,223,1006,224,614,101,1,223,223,7,226,226,224,102,2,223,223,1005,224,629,1001,223,1,223,1108,677,677,224,102,2,223,223,1006,224,644,1001,223,1,223,8,677,226,224,1002,223,2,223,1005,224,659,101,1,223,223,1108,226,677,224,102,2,223,223,1005,224,674,101,1,223,223,4,223,99,226]

		# test A/C (1)
		f = process_instructions(instructions.copy(),1)
		print (f[0])	

		print()
		# test Thermal Radiators (5)
		f = process_instructions(instructions.copy(),5)
		print (f[0])	

	print("done");




def puzzle_text():
	print("""
--- Day 5: Sunny with a Chance of Asteroids ---
You're starting to sweat as the ship makes its way toward Mercury. The Elves suggest that you get the air conditioner working by upgrading your ship computer to support the Thermal Environment Supervision Terminal.

The Thermal Environment Supervision Terminal (TEST) starts by running a diagnostic program (your puzzle input). The TEST diagnostic program will run on your existing Intcode computer after a few modifications:

First, you'll need to add two new instructions:

Opcode 3 takes a single integer as input and saves it to the position given by its only parameter. For example, the instruction 3,50 would take an input value and store it at address 50.
Opcode 4 outputs the value of its only parameter. For example, the instruction 4,50 would output the value at address 50.
Programs that use these instructions will come with documentation that explains what should be connected to the input and output. The program 3,0,4,0,99 outputs whatever it gets as input, then halts.

Second, you'll need to add support for parameter modes:

Each parameter of an instruction is handled based on its parameter mode. Right now, your ship computer already understands parameter mode 0, position mode, which causes the parameter to be interpreted as a position - if the parameter is 50, its value is the value stored at address 50 in memory. Until now, all parameters have been in position mode.

Now, your ship computer will also need to handle parameters in mode 1, immediate mode. In immediate mode, a parameter is interpreted as a value - if the parameter is 50, its value is simply 50.

Parameter modes are stored in the same value as the instruction's opcode. The opcode is a two-digit number based only on the ones and tens digit of the value, that is, the opcode is the rightmost two digits of the first value in an instruction. Parameter modes are single digits, one per parameter, read right-to-left from the opcode: the first parameter's mode is in the hundreds digit, the second parameter's mode is in the thousands digit, the third parameter's mode is in the ten-thousands digit, and so on. Any missing modes are 0.

For example, consider the program 1002,4,3,4,33.

The first instruction, 1002,4,3,4, is a multiply instruction - the rightmost two digits of the first value, 02, indicate opcode 2, multiplication. Then, going right to left, the parameter modes are 0 (hundreds digit), 1 (thousands digit), and 0 (ten-thousands digit, not present and therefore zero):

ABCDE
 1002

DE - two-digit opcode,      02 == opcode 2
 C - mode of 1st parameter,  0 == position mode
 B - mode of 2nd parameter,  1 == immediate mode
 A - mode of 3rd parameter,  0 == position mode,
                                  omitted due to being a leading zero
This instruction multiplies its first two parameters. The first parameter, 4 in position mode, works like it did before - its value is the value stored at address 4 (33). The second parameter, 3 in immediate mode, simply has value 3. The result of this operation, 33 * 3 = 99, is written according to the third parameter, 4 in position mode, which also works like it did before - 99 is written to address 4.

Parameters that an instruction writes to will never be in immediate mode.

Finally, some notes:

It is important to remember that the instruction pointer should increase by the number of values in the instruction after the instruction finishes. Because of the new instructions, this amount is no longer always 4.
Integers can be negative: 1101,100,-1,4,0 is a valid program (find 100 + -1, store the result in position 4).
The TEST diagnostic program will start by requesting from the user the ID of the system to test by running an input instruction - provide it 1, the ID for the ship's air conditioner unit.

It will then perform a series of diagnostic tests confirming that various parts of the Intcode computer, like parameter modes, function correctly. For each test, it will run an output instruction indicating how far the result of the test was from the expected value, where 0 means the test was successful. Non-zero outputs mean that a function is not working correctly; check the instructions that were run before the output instruction to see which one failed.

Finally, the program will output a diagnostic code and immediately halt. This final output isn't an error; an output followed immediately by a halt means the program finished. If all outputs were zero except the diagnostic code, the diagnostic program ran successfully.

After providing 1 to the only input instruction and passing all the tests, what diagnostic code does the program produce?

Your puzzle answer was 11193703.

--- Part Two ---
The air conditioner comes online! Its cold air feels good for a while, but then the TEST alarms start to go off. Since the air conditioner can't vent its heat anywhere but back into the spacecraft, it's actually making the air inside the ship warmer.

Instead, you'll need to use the TEST to extend the thermal radiators. Fortunately, the diagnostic program (your puzzle input) is already equipped for this. Unfortunately, your Intcode computer is not.

Your computer is only missing a few opcodes:

Opcode 5 is jump-if-true: if the first parameter is non-zero, it sets the instruction pointer to the value from the second parameter. Otherwise, it does nothing.
Opcode 6 is jump-if-false: if the first parameter is zero, it sets the instruction pointer to the value from the second parameter. Otherwise, it does nothing.
Opcode 7 is less than: if the first parameter is less than the second parameter, it stores 1 in the position given by the third parameter. Otherwise, it stores 0.
Opcode 8 is equals: if the first parameter is equal to the second parameter, it stores 1 in the position given by the third parameter. Otherwise, it stores 0.
Like all instructions, these instructions need to support parameter modes as described above.

Normally, after an instruction is finished, the instruction pointer increases by the number of values in that instruction. However, if the instruction modifies the instruction pointer, that value is used and the instruction pointer is not automatically increased.

For example, here are several programs that take one input, compare it to the value 8, and then produce one output:

3,9,8,9,10,9,4,9,99,-1,8 - Using position mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
3,9,7,9,10,9,4,9,99,-1,8 - Using position mode, consider whether the input is less than 8; output 1 (if it is) or 0 (if it is not).
3,3,1108,-1,8,3,4,3,99 - Using immediate mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
3,3,1107,-1,8,3,4,3,99 - Using immediate mode, consider whether the input is less than 8; output 1 (if it is) or 0 (if it is not).
Here are some jump tests that take an input, then output 0 if the input was zero or 1 if the input was non-zero:

3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9 (using position mode)
3,3,1105,-1,9,1101,0,0,12,4,12,99,1 (using immediate mode)
Here's a larger example:

3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,
1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,
999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99
The above example program uses an input instruction to ask for a single number. The program will then output 999 if the input value is below 8, output 1000 if the input value is equal to 8, or output 1001 if the input value is greater than 8.

This time, when the TEST diagnostic program runs its input instruction to get the ID of the system to test, provide it 5, the ID for the ship's thermal radiator controller. This diagnostic test suite only outputs one number, the diagnostic code.

What is the diagnostic code for system ID 5?

Your puzzle answer was 12410607.

Both parts of this puzzle are complete! They provide two gold stars: **
""")
