def part2(data): #part2 phases = list(itertools.permutations([5, 6, 7, 8, 9])) sol = [] for p in phases: _s = 0 q = [] c = [] j = [] for i in range(5): _q = Queue() _q.put(p[i]) q.append(_q) q[0].put(0) for i in range(5): cc = ic.CPU("CPU_" + str(i), _data, q[i], q[(i + 1) % 5]) w = Process(target=interpret_mp, args=(cc, q[i], q[(i + 1) % 5])) c.append(cc) j.append(w) w.start() while len(j) > 0: j = [jj for jj in j if jj.is_alive()] time.sleep(0.1) sol.append(c[4]._out.get()) print("------------------") print(max(sol))
def part1(data): #part1 phases = list(itertools.permutations([0, 1, 2, 3, 4])) sol = [] for p in phases: _s = 0 for i in range(5): q1 = Queue() q1.put(p[i]) q1.put(_s) q2 = Queue() c = ic.CPU("CPU_" + str(i), data, q1, q2) c.interpret() _s = q2.get() sol.append(_s) print(max(sol))
def paint(code, start): image = defaultdict(int) x, y = 0, 0 image[(x, y)] = start direction = 0 _in = mp.Queue() _out = mp.Queue() vm = ic.CPU("Part1", code, _in, _out) w = mp.Process(target=runvm, args=(vm, _in, _out)) w.start() while w.is_alive(): _in.put(image[(x, y)]) color = _out.get() turn = _out.get() image[(x, y)] = color direction = (direction + turn * 2 - 1) % 4 x += (0, 1, 0, -1)[direction] y += (-1, 0, 1, 0)[direction] if (x, y) == (41, 4): break w.terminate() return image
import itertools import time from intcode import intcode as ic if __name__=="__main__": _memory = [0] * 64 * 1024 * 1024 _data = [int(x) for x in open("d09.in").read().split(",")] #_data = [int(x) for x in "1102,34915192,34915192,7,4,7,99,0".split(",")] _memory[0:len(_data)] = _data outq = ic.Queue() c = ic.CPU("Part1", _memory, ic.Queue([1]), outq) c.interpret() print(outq.get()) c = ic.CPU("Part2", _memory, ic.Queue([2]), outq) c.interpret() print(outq.get())
from intcode import intcode as ic from collections import defaultdict if __name__ == "__main__": _memory = [0] * 64 * 1024 * 1024 _data = [int(x) for x in open("d17.in").read().split(",")] _memory[0:len(_data)] = _data cpu = ic.CPU("Part1", _memory, ic.Queue(), ic.Queue()) scr = [["."] * 50 for _ in range(50)] count = 0 for i in range(50): for j in range(50): cpu._in.put(j) cpu._in.put(i) cpu.interpret() d = chr(cpu._out.get()) if d == '1': count += 1 scr[i][j] = d for l in scr: print("".join(l)) print(count)
vm._in.put(revs[_m]) vm.interpret() vm._out.get() if p == 2: m[crt] = "*" print(l) if __name__ == "__main__": _memory = [0] * 4 * 1024 * 1024 _data = [int(x) for x in open("d15.in").read().split(",")] _memory[0:len(_data)] = _data _in = ic.Queue() _out = ic.Queue() vm = ic.CPU("Part1", _memory, _in, _out) N = 60 crt = (N, N) l = 0 m = {crt: "0"} search(m, crt, 0, vm) #print(m) img = [[" "] * (2 * N) for _ in range(2 * N)] for k in m.keys(): x, y = k v = m[k] if v == 0: v = " " if v not in ["*", "#"] and int(v) > 0:
print("starting:", i) _cpu.interpret() print("done", i) if __name__ == "__main__": _memory = [0] * 4 * 1024 * 1024 _data = [int(x) for x in open("d23.in").read().split(",")] _memory[0:len(_data)] = _data code = _memory cpus = [] queues = [] for i in range(50): q = (mp.Queue(), mp.Queue()) queues.append(q) _cpu = ic.CPU(str(i), _memory, q[0], q[1]) w = mp.Process(target=runvm, args=(_cpu, )) cpus.append(w) w.start() packets = [] while True: for i in range(50): print(".", ) if not queues[i][1].empty(): p = queues[i][1].get_nowait() print(".", ) if p == 255: print(queues[i][1].get()) exit(0) x = q[i][1].get() print("{} -> {} {}".format(i, p, x))