Problem Description
This is the Interpreter problem. The idea is it's a computer where a list of commands are read from RAM and processed with a set of instructions and registers.
The input file is a list of commands read into RAM that are then processed according to these rules
Solution
I have main do a little bit of the work in this problem. It basically iterates through the commands in RAM, the trouble is some of the commands change the instruction pointer to a new location in RAM. I feel like my logic is a little messy with those tasks. There is a class called RamProcessor that handles each command, but the instance also keeps track of the instruction pointer.
I just treated the commands as regular integers, and then every result was processed as modulo 1000 to keep it to three digits, or roll-over. class RamProcessor: def __init__(self): self.register = [0 for i in range(10)] self.ram = [0 for i in range(1000)] self.instruction = 0 self.instruction_digit1 = 0 self.instruction_digit2 = 0 self.instruction_digit3 = 0 self.ram_counter = 0 def read_ram(self): file_reader = fr.FileReader("inputFile.txt") self.ram = file_reader.file_as_int_list() return self.ram def instruction_router(self, instruction): self.instruction = instruction self.instruction_digit1 = int(self.instruction/100) self.instruction_digit2 = int((self.instruction % 100)/10) self.instruction_digit3 = int(((self.instruction % 100)% 10)) match self.instruction_digit1: case 0: if self.register[self.instruction_digit3] == 0: return 100 else: self.instruction = self.ram[self.register[self.instruction_digit2]] self.ram_counter = self.register[self.instruction_digit2] case 1: print("halt") case 2: self.register[self.instruction_digit2] = self.instruction_digit3 self.register[self.instruction_digit2] = (self.register[self.instruction_digit2] % 1000) case 3: self.register[self.instruction_digit2] += self.instruction_digit3 self.register[self.instruction_digit2] = (self.register[self.instruction_digit2] % 1000) case 4: self.register[self.instruction_digit2] *= self.instruction_digit3 self.register[self.instruction_digit2] = (self.register[self.instruction_digit2] % 1000) case 5: self.register[self.instruction_digit2] = self.register[self.instruction_digit3] self.register[self.instruction_digit2] = (self.register[self.instruction_digit2] % 1000) case 6: self.register[self.instruction_digit2] += self.register[self.instruction_digit3] self.register[self.instruction_digit2] = (self.register[self.instruction_digit2] % 1000) case 7: self.register[self.instruction_digit2] *= self.register[self.instruction_digit3] self.register[self.instruction_digit2] = (self.register[self.instruction_digit2] % 1000) case 8: self.register[self.instruction_digit2] = self.ram[self.register[self.instruction_digit3]] self.register[self.instruction_digit2] = (self.register[self.instruction_digit2] % 1000) case 9: self.ram[self.instruction_digit3] = self.register[self.instruction_digit2] self.ram[self.instruction_digit3] = (self.ram[self.instruction_digit3] % 1000) return 0 def get_ram_contents(self): return self.ram def update_ram_counter(self): return self.ram_counter
See full code on my GitHub
0 Comments
Leave a Reply. |
Archives
December 2022
Categories |