def intcode_worker(program, inqueue, outqueue): qin = QueueIOObj(inqueue) qout = QueueIOObj(outqueue) r, output = compute(program, instream=qin, outstream=qout) outqueue.put(9) outqueue.put(9) return output[-1]
def find_input(desired_result, initial_program): for noun in range(0, 100): for verb in range(0, 100): program = initial_program.copy() program[1] = noun program[2] = verb result = intcode.compute(program) if result == desired_result: return noun * 100 + verb
def compute_signal(phase): phase_settings = phase[:] prg_output = 0 while len(phase_settings) > 0: inp_string = "%d\n%d\n" % (phase_settings.pop(0), prg_output) # print(inp_string) x = StringIO(inp_string) retval, prg_output = compute(program[:], instream=x, outstream=sys.stdout) prg_output = prg_output[-1] # print(prg_output) return prg_output
import intcode text = intcode.readFile("day9.txt") # text = [109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99] # text = [1102,34915192,34915192,7,4,7,99,0] # text = [104,1125899906842624,99] intcode.compute(text) # def intcode(text): # # inputIndex = 0 # relBase = 0 # i = 0 # while i < len(text): # # get values from current position # opcode = text[i] % 100 # modes = [] # vals = [] # params = text[i] // 100 # paramNum = 0 # # set paramNum and parameters based on opcode # if opcode in [1,2,7,8]: # paramNum = 3 # elif opcode in [3,4,9]: # paramNum = 1 # elif opcode in [5,6]: # paramNum = 2 # for p in range(paramNum): # modes.append(params % 10)
import intcode text = intcode.readFile("day13.txt") # 0 = empty tile # 1 = wall tile (indestructible) # 2 = block tile (destructible) # 3 = horizontal paddle (indestructible) # 4 = ball # memory position 0 = number of quarters inserted text[0] = 2 # x y tile_id output = intcode.compute(text) screen = [] for i in range(0, len(output), 3): if output[i + 2] == 2 and (output[i], output[i + 1]) not in screen: screen.append((output[i], output[i + 1])) print(len(screen))
def test(a, b): prg = program_base[:] prg[1] = a prg[2] = b return compute(prg)
def worker(name, inqueue, outqueue): qin = QueueIOObj(inqueue) qout = QueueIOObj(outqueue) r, output = compute(program, instream=qin, outstream=qout) return output[-1]
import sys from intcode import compute program = [int(i) for i in input().strip().split(',')] print(compute(program))
#!/usr/bin/env python3 import intcode from copy import copy with open("input", "r") as f: program = list(map(int, f.read().split(","))) program_modified = copy(program) program_modified[1] = 12 program_modified[2] = 2 print(intcode.compute(program_modified))
for row in range(height)] # 0 is black, 1 is white i = 0 directions = ["up", "left", "down", "right"] curRow = 2 curCol = 2 curDir = directions[0] paintedCells = [] for x in range(20): # print(curRow, curCol, height, width) inputNum = int(not grid[curRow][curCol] == 0) output, text, i = intcode.compute(text, inputNum, i) if output == "HALT": break grid[curRow][curCol] = output[0] if (curRow, curCol) not in paintedCells: paintedCells.append((curRow, curCol)) if output[1] == 0: curDir = directions[(directions.index(curDir) + 1) % len(directions)] else: newIndex = directions.index(curDir) - 1 if newIndex < 0: newIndex = len(directions) - 1 curDir = directions[newIndex]
def part1(input): mem = inputreader.to_intcode(input) data, outs = intcode.compute(mem, [1]) return outs
def to_dict(arr): return {i: arr[i] for i in range(0, len(arr))} def read_input(): f = open("input") a = list(map(int, f.readlines()[0].split(","))) f.close() return to_dict(a) #print(read_input()) assert (compute( Program( to_dict([ 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99 ]))) == [ 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99 ]) assert (compute(Program(to_dict([1102, 34915192, 34915192, 7, 4, 7, 99, 0]))) == [34915192 * 34915192]) assert (compute(Program(to_dict([104, 1125899906842624, 99]))) == [1125899906842624]) print("Part 1:", compute(Program(read_input()), 1)) print("Part 2:", compute(Program(read_input()), 2))
def amplifier(program, input, mode=None): output = compute(program, input, mode) if output is not None and len(output) > 0: return output[0] return None