Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from config import file_path
- def read_file():
- with open(file_path) as f:
- lines = f.readlines()
- return lines
- def add_int(int_program, program_counter, first_par, second_par, third_par):
- if(first_par):
- first_val = int(int_program[program_counter + 1])
- else:
- first_val = int_program[int(int_program[program_counter + 1])]
- if(second_par):
- second_val = int(int_program[program_counter + 2])
- else:
- second_val = int_program[int(int_program[program_counter + 2])]
- if(third_par):
- adress = int(int_program[program_counter + 3])
- else:
- adress = int(int_program[program_counter + 3])
- int_program[int(adress)] = int(first_val) + int(second_val)
- return int_program
- def mult_int(int_program, program_counter, first_par, second_par, third_par):
- if(first_par):
- first_val = int(int_program[program_counter + 1])
- else:
- first_val = int_program[int(int_program[program_counter + 1])]
- if(second_par):
- second_val = int(int_program[program_counter + 2])
- else:
- second_val = int_program[int(int_program[program_counter + 2])]
- if(third_par):
- adress = int(int_program[program_counter + 3])
- else:
- adress = int(int_program[program_counter + 3])
- int_program[int(adress)] = int(first_val) * int(second_val)
- return int_program
- def read_op(int_program, program_counter, first_par, second_par, third_par):
- if(first_par):
- read_val = int_program[program_counter + 1]
- else:
- read_val = int_program[int(int_program[program_counter + 1])]
- #print("R: ", read_val)
- return read_val
- def write_op(int_program, program_counter, input, first_par, second_par, third_par):
- int_program[int(int_program[program_counter + 1])] = input
- return int_program
- def jump_true(int_program, program_counter, first_par, second_par):
- if(first_par):
- first_val = int(int_program[program_counter + 1])
- else:
- first_val = int_program[int(int_program[program_counter + 1])]
- if(second_par):
- second_val = int(int_program[program_counter + 2])
- else:
- second_val = int_program[int(int_program[program_counter + 2])]
- if(int(first_val) > 0):
- program_counter = second_val
- return program_counter
- else:
- return program_counter + 3
- def jump_false(int_program, program_counter, first_par, second_par, third_par):
- if(first_par):
- first_val = int(int_program[program_counter + 1])
- else:
- first_val = int_program[int(int_program[program_counter + 1])]
- if(second_par):
- second_val = int(int_program[program_counter + 2])
- else:
- second_val = int_program[int(int_program[program_counter + 2])]
- if(int(first_val) == 0):
- program_counter = second_val
- return program_counter
- else:
- return program_counter + 3
- def less_than(int_program, program_counter, first_par, second_par, third_par):
- if(first_par):
- first_val = int(int_program[program_counter + 1])
- else:
- first_val = int_program[int(int_program[program_counter + 1])]
- if(second_par):
- second_val = int(int_program[program_counter + 2])
- else:
- second_val = int_program[int(int_program[program_counter + 2])]
- if(third_par):
- adress = int(int_program[program_counter + 3])
- else:
- adress = int(int_program[program_counter + 3])
- if(int(first_val) < int(second_val)):
- int_program[adress] = 1
- else:
- int_program[adress] = 0
- return int_program
- def equal(int_program, program_counter, first_par, second_par, third_par):
- if(first_par):
- first_val = int(int_program[program_counter + 1])
- else:
- first_val = int_program[int(int_program[program_counter + 1])]
- if(second_par):
- second_val = int(int_program[program_counter + 2])
- else:
- second_val = int_program[int(int_program[program_counter + 2])]
- if(third_par):
- adress = int(int_program[program_counter + 3])
- else:
- adress = int(int_program[program_counter + 3])
- if(int(first_val) == int(second_val)):
- int_program[adress] = 1
- else:
- int_program[adress] = 0
- return int_program
- def run_int_program(int_program, sequence_num, read_val):
- program_counter = 0
- is_squence = True
- while(1):
- index = int(int_program[program_counter])
- first_par = False
- second_par = False
- third_par = False
- if(len(str(index)) > 1):
- if(len(str(index)) == 5):
- if(str(index)[0] == '1'):
- third_par = True
- if(str(index)[1] == '1'):
- second_par = True
- if(str(index)[2] == '1'):
- first_par = True
- index = str(index)[3] + str(index)[4]
- index = int(index)
- elif(len(str(index)) == 4):
- if(str(index)[1] == '1'):
- first_par = True
- if(str(index)[0] == '1'):
- second_par = True
- index = str(index)[len(str(index)) - 2] + str(index)[len(str(index)) - 1]
- index = int(index)
- elif(len(str(index)) == 3):
- if(str(index)[0] == '1'):
- first_par = True
- index = str(index)[len(str(index)) - 2] + str(index)[len(str(index)) - 1]
- index = int(index)
- if(index == 1):
- int_program = add_int(int_program, program_counter, first_par, second_par, third_par)
- program_counter = program_counter + 4
- elif(index == 2):
- int_program = mult_int(int_program, program_counter, first_par, second_par, third_par)
- program_counter = program_counter + 4
- elif(index == 3):
- #print(is_squence, sequence_num, read_val)
- if(is_squence):
- int_program = write_op(int_program, program_counter, sequence_num, first_par, second_par, third_par)
- is_squence = False
- else:
- int_program = write_op(int_program, program_counter, read_val, first_par, second_par, third_par)
- is_squence = True
- program_counter = program_counter + 2
- elif(index == 4):
- read_val = read_op(int_program, program_counter, first_par, second_par, third_par)
- program_counter = program_counter + 2
- elif(index == 5):
- program_counter = jump_true(int_program, program_counter, first_par, second_par)
- program_counter = int(program_counter)
- elif(index == 6):
- program_counter = jump_false(int_program, program_counter, first_par, second_par, third_par)
- program_counter = int(program_counter)
- elif(index == 7):
- int_program = less_than(int_program, program_counter, first_par, second_par, third_par)
- program_counter = program_counter + 4
- elif(index == 8):
- int_program = equal(int_program, program_counter, first_par, second_par, third_par)
- program_counter = program_counter + 4
- elif(index == 99):
- return read_val
- else:
- print("Wtf", index)
- return
- def main():
- int_program = read_file()
- int_program = int_program[0]
- int_program = int_program.split(",")
- max_val = 0
- r_val = 0
- seq = []
- for one in range(0,5):
- for two in range(0,5):
- for three in range(0,5):
- for four in range(0,5):
- for five in range(0,5):
- sequence = [one,two,three,four,five]
- r_val = 0
- for i in sequence:
- r_val = run_int_program(int_program, i, r_val)
- if(r_val > max_val):
- print(r_val, ">", max_val)
- max_val = r_val
- seq = sequence
- print("Max Value: ", max_val, seq)
- if __name__== "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement