def __init__(self, filename): self.nums = read_int_list(filename)
command = self.read_op_code() self.execute_command(command) return self.output # below code is added for day 7 usage def set_phase(self, phase): self.input.append(phase) def step(self, input: int): """ This mode is used in day 7 code, it just starts with an input and stops when an output is generated, it will be returned with a new input later, this will go on till the computer halts! """ self.input.append(input) while not (self.output or self.halted): command = self.read_op_code() self.execute_command(command) if self.halted: return None else: return self.output.pop() if __name__ == '__main__': prog = read_int_list('input.txt') computer = IntcodeComputer(prog) print(computer.compute(deque([1]))) print(computer.compute(deque([5])))
For day 11 and 13 code, this has been changed to output multiple codes """ if input is not None: self.input.append(input) while not self.halted: command = self.read_op_code() if (command[0] == 3) and (len(self.input) == 0): break self.execute_command(command) out = self.output self.output = deque() return out if __name__ == '__main__': test1 = read_int_list('test1.txt') computer_test1 = IntcodeComputer(test1) assert computer_test1.compute(deque()) == deque(test1) test2 = read_int_list('test2.txt') computer_test2 = IntcodeComputer(test2) test_output = computer_test2.compute(deque()) assert len(str(test_output.pop())) == 16 test3 = read_int_list('test3.txt') computer_test3 = IntcodeComputer(test3) assert computer_test3.compute(deque()).pop() == 1125899906842624 prog = read_int_list('input.txt') computer = IntcodeComputer(prog) print(computer.compute(deque([1])))
from myutils.file_reader import read_int_list values = read_int_list('input.txt') # first challenge, using dictionary d = {} for n in values: if d.get(n, None) is not None: print(n * d[n]) d[2020 - n] = n # second challenge, using double dictionaries d = {} dd = {} for n in values: if dd.get(n, None) is not None: print(n * dd[n]) for k, v in d.items(): dd[k - n] = v * n d[2020 - n] = n # second challenge, using brute-force looping! for i in range(len(values)): for j in range(i + 1, len(values)): for k in range(j + 1, len(values)): if values[i] + values[j] + values[k] == 2020: print(values[i] * values[j] * values[k])
from myutils.file_reader import read_int_list lines = read_int_list('input.txt') s = 0 s2 = 0 for n in lines: f = max(0, n // 3 - 2) s += f s2 += f while f > 0: f = max(0, f // 3 - 2) s2 += f print(s, s2)
def __init__(self, filename): self.nums = list(map(int, read_int_list(filename)))
def __init__(self, filename): self.program = read_int_list(filename)
def __init__(self, filename): self.nums = read_int_list(filename) self.count = len(self.nums) self.process()
def __init__(self, filename): self.nums = read_int_list(filename) self.process_fuel_needed()
def __init__(self, filename): self.program = read_int_list(filename) self.computer = IntcodeComputer(self.program.copy())
def __init__(self, filename): input = read_int_list(filename) self.card_public_key, self.door_public_key = input[0], input[1]