def getTilesAffectedByRayInRadius50(code): ic = intcodeComputer.IntcodeComputer(code) inputs = [] size = 50 for x in range(size): for y in range(size): inputs += [x, y] icState = ic.getState() outputs = [["." for _ in range(size)] for _ in range(size)] totalAffected = 0 for i in range(0, len(inputs), 2): ic.setState(*icState) ic.setInputs(inputs[i:i + 2]) x, y = ic.inputs while not ic.finished: out = ic.step() if out is not None: outputs[y][x] = "#" if out else "." totalAffected += out print "\n".join([str(i) + "".join(x) for i, x in enumerate(outputs)]) print "Total affected (Part 1): ", totalAffected return outputs
def firstPart(opcodes): # Prepare input according to task instructions opcodes[1] = 12 opcodes[2] = 2 ic = intcodeComputer.IntcodeComputer(opcodes) ic.compute() return ic.code[0]
def secondPart(opcodes): for noun in range(100): for verb in range(100): currentOpcodesRun = list(opcodes) currentOpcodesRun[1] = noun currentOpcodesRun[2] = verb ic = intcodeComputer.IntcodeComputer(currentOpcodesRun) ic.compute() output = ic.code[0] if output == 19690720: return 100 * noun + verb return -1
_input = f.read() code = list(map(int, _input.split(","))) # Part 1 outputs50 = getTilesAffectedByRayInRadius50(code) # Part 2 # # Find an approximation of the slope of the ray # # # Find the affected tiles at Y 50 affectedAtY50 = [x for x in range(50) if outputs50[-1][x] == "#"] # # # Find the one to the left firstOneAtY50 = [affectedAtY50[0], 50] slope = firstOneAtY50[1] / float(firstOneAtY50[0]) ic = intcodeComputer.IntcodeComputer(code) icState = ic.getState() currentRow = 100 # Start at row 100, as it's obvious the solution is not before that step = 50 lastComboWorks = False squareSize = 100 while True: firstAffectedOnThisRow = findFirstAffectedOnRow( ic, icState, int(currentRow / slope), currentRow) numAffectedOnThisRow = getNumberAffectedOnRow(ic, icState, firstAffectedOnThisRow, currentRow) if numAffectedOnThisRow < squareSize:
for offset, x in enumerate(path): if x not in ignoreList + [letter]: break path, newChunks = compress(path, offset, ignoreList + [letter]) chunks += newChunks return path, chunks if __name__ == "__main__": with open("data/17_data", "r") as f: _input = f.read() code = list(map(int, _input.split(","))) ic = intcodeComputer.IntcodeComputer(list(code)) chars = {10: "\n", 35: "#", 46: ".", 94: "^", 60: "<", 62: ">", 118: "v"} _map = [[]] mapStr = "" while not ic.finished: out = ic.step() if out is not None: mapStr += chars[out] if out == 10: _map.append([]) else: _map[-1].append(chars[out])
if idle: print "Restart after idle" inputs[0] = list(NATpacket) sentFromNAT += NATpacket if len(sentFromNAT ) > 2 and sentFromNAT[-1] == sentFromNAT[-3]: print("First repeated Y value sent to 0 from NAT", sentFromNAT[-1]) alive = False return if len(outputBuffers[_id]) == 3: address, x, y = outputBuffers[_id] inputs[address] += [x, y] outputBuffers[_id] = [] ic = [] for i in range(numComputers): ic.append(intcodeComputer.IntcodeComputer(code)) ic[-1].setInputCallback(partial(inputCallback, i)) ic[-1].setOutputCallback(partial(outputCallback, i)) inputs.append([i]) outputBuffers.append([]) while alive: for computer in ic: computer.step()
strInput = INSTRUCTIONS.pop(0) + "\n" else: strInput = raw_input() + "\n" print(strInput) inputBuffer = [ord(x) for x in strInput] if strInput[:-1] == "north": pos[1] += 1 elif strInput[:-1] == "south": pos[1] -= 1 elif strInput[:-1] == "east": pos[0] += 1 elif strInput[:-1] == "west": pos[0] -= 1 tiles[tuple(pos)] = "." return inputBuffer.pop(0) ic = intcodeComputer.IntcodeComputer(code, inputCallback=inputCallback) while not ic.finished: out = ic.step() if out is not None: outputBuffer.append(chr(out)) if ic.finished: print("".join(outputBuffer)) print("FINISHED")