def day19(): arg = get_input() num = get_numbers(arg) import intcode pro = intcode.process(num, printt=False) f = fifo() pro.out(f) d = dict() def fu(x, y): if (x, y) in d: return d[(x, y)] pro.reset() pro.feed(x) pro.feed(y) pro.run_until_output() d[(x, y)] = f.get() return d[(x, y)] print(sum(fu(x, y) for x, y in itertools.product(range(50), range(50)))) x = 30 y = 0 stop = False while not stop: while fu(x, y) == 0: y += 1 xx, yy = x - 99, y + 99 if fu(x, y) + fu(xx, yy) == 2: print(x - 99, y, 10000 * (x - 99) + y) stop = True x += 1
def day9(): import intcode arg = get_input() num = get_numbers(arg) n = len(num) pro = intcode.process(num) pro.feed(2) pro.run_until_stop()
def day11(): import intcode arg = get_input() num = get_numbers(arg) pro = intcode.process(num, printt = False) x,y = 0,0 direction = "up" grid = dict() s = 0 out = fifo() pro.out(out) grid[(0,0)] = 1 #part2 while pro.alive: if (x,y) not in grid: s += 1 grid[(x,y)] = 0 pro.feed(grid[(x,y)]) pro.run_until_output() pro.run_until_output() if not pro.alive: break color = out.get_nowait() d = out.get_nowait() grid[(x,y)] = color if d == 0: #left direction = {"up":"left", "left":"down", "down":"right", "right":"up"}[direction] else: #right direction = {"up":"right", "right":"down", "down":"left", "left":"up"}[direction] x += {"left":-1, "right":1, "up":0, "down":0}[direction] y += {"left":0, "right":0, "up":-1, "down":1}[direction] xa, xb = min(x for (x,y) in grid), max(x for (x,y) in grid) ya, yb = min(y for (x,y) in grid), max(y for (x,y) in grid) L = [[0 for i in range(xa, xb+1)] for j in range(ya, yb+1)] for x,y in grid: L[y-ya][x-xa] = grid[(x,y)] plt.matshow(L)
def day13(): arg = get_input() num = get_numbers(arg) import intcode pro = intcode.process(num, printt = False) pro.regs[0] = 2 f = fifo() pro.out(f) g = dict() score = 0 bx, by = 0,0 px,py = 0,0 while pro.alive: pro.run_until_stop() while not f.empty(): x = f.get_nowait() y = f.get_nowait() t = f.get_nowait() if (x,y) == (-1,0): score = t else: g[(x,y)] = t if t == 4: bx,by = x,y if t == 3: px,py = x,y if bx < px: pro.feed(-1) elif bx > px: pro.feed(1) else: pro.feed(0) print(score)
def day7(): import intcode arg = get_input() num = get_numbers(arg) n = len(num) pro = [intcode.process(num) for k in range(5)] for k in range(4): pro[k].out(pro[k+1]) pro[4].out(pro[0]) output_last = list() pro[4].out(output_last) s = set() for perm in itertools.permutations((0,1,2,3,4)): for k in range(5): pro[k].reset() pro[k].feed(perm[k]+5) pro[0].feed(0) alive = True while alive: for k in range(5): pro[k].run_until_stop() alive = False for k in range(5): alive = alive or pro[k].alive s.add(output_last[-1]) print(max(s))
def find_key_code(): print("Part 1:") intcode.process(1, True)
def find_coordinates(): print("Part 2:") intcode.process(2, True)
import intcode with open('input.txt') as f: data = list(map(int, f.readline().split(","))) # Part 1: intcode = intcode.IntCode(data) #intcode.process(1, True) # Part 2: intcode.process(5, True)
def day17(): arg = get_input() num = get_numbers(arg) import intcode pro = intcode.process(num, printt=False) f = fifo() pro.out(f) pro.run_until_stop() grid = dict() x, y = 0, 0 xr, yr = 0, 0 d = "" while not f.empty(): c = f.get_nowait() if c in (35, 46): grid[(x, y)] = chr(c) elif c in (60, 62, 94, 118): xr, yr = x, y d = {60: "l", 62: "r", 94: "u", 118: "d"}[c] grid[(x, y)] = '#' x, y = (0, y + 1) if c == 10 else (x + 1, y) s = 0 for a, c in grid.items(): x, y = a if c == "#": ok = True for xx, yy in ((x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)): if not ((xx, yy) in grid and grid[(xx, yy)] == '#'): ok = False if ok: s += x * y print("p1 :", s) path = [] x, y = xr, yr px, py = -1, -1 while 1: rx, ry = { "r": (x + 1, y), "l": (x - 1, y), "d": (x, y + 1), "u": (x, y - 1) }[turn_right(d)] lx, ly = { "r": (x + 1, y), "l": (x - 1, y), "d": (x, y + 1), "u": (x, y - 1) }[turn_left(d)] if (rx, ry) in grid and grid[(rx, ry)] == '#': d = turn_right(d) pd = "R" elif (lx, ly) in grid and grid[(lx, ly)] == '#': d = turn_left(d) pd = "L" else: break pl = 0 while (x, y) in grid and grid[(x, y)] == '#': pl += 1 px, py = x, y x, y = { "r": (x + 1, y), "l": (x - 1, y), "d": (x, y + 1), "u": (x, y - 1) }[d] x, px = px, px - (x - px) y, py = py, py - (y - py) path.append(pd + "," + str(pl - 1)) n = len(path) for sizes in itertools.product(range(2, 21), range(2, 21), range(2, 21)): p = ',' + ','.join(path) patterns = [] for k in (0, 1, 2): pattern = p[:sizes[k]] patterns.append(pattern) p = p.replace(pattern, "") if p.replace(',', '') == "": print(sizes) break p = ','.join(path) for k in range(3): patterns[k] = patterns[k][1:] p = p.replace(patterns[k], "ABC"[k]) pro.reset() pro.regs[0] = 2 for line in [p] + patterns + ["n"]: for c in line: pro.feed(ord(c)) pro.feed(10) pro.run_until_stop() while not f.empty(): c = f.get() if c > 200: print("p2 :", c)
cost = make(m, "FUEL") while cost < trillion: m += 1 cost += make(1, "FUEL") print("p2 :", m-1) arg = get_input() num = get_numbers(arg) import intcode pro = intcode.process(num, printt=False) f = fifo() pro.out(f) grid = dict() grid[(0,0)] = 1 x,y = 0,0 pile = lifo() pile.put((0,0)) routing = dict() while not pile.empty(): target = pile.get() while 1:
import intcode def permutations(elements): if len(elements) <= 1: yield elements else: for perm in permutations(elements[1:]): for i in range(len(elements)): yield perm[:i] + elements[0:1] + perm[i:] with open("inputs/day7.txt", "r") as f: for line in f: l = intcode.process(line) largest_output = 0 max_phase = [] perms = list(permutations([0, 1, 2, 3, 4])) for i in range(0, len(perms)): in1 = intcode.execute_ret_output(l, [0, perms[i][0]]) in2 = intcode.execute_ret_output(l, [in1, perms[i][1]]) in3 = intcode.execute_ret_output(l, [in2, perms[i][2]]) in4 = intcode.execute_ret_output(l, [in3, perms[i][3]]) in5 = intcode.execute_ret_output(l, [in4, perms[i][4]]) if in5 > largest_output: max_phase = perms[i] largest_output = in5 print("phase: " + str(max_phase) + " output: " + str(largest_output))