def main(): # part 1 memory_ = numpy.loadtxt('day9\input.txt', delimiter=',', dtype=numpy.int64) #memory_ = numpy.fromstring('1102,34915192,34915192,7,4,7,99,0', sep=',', dtype=numpy.int64) #memory_ = numpy.fromstring('104,1125899906842624,99', sep=',', dtype=numpy.int64) #memory_ = numpy.fromstring('109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99', sep=',', dtype=numpy.int64) ### Part 1 memory = numpy.array(memory_, copy=True) cpu = CPU(memory) cpu.queue_input(1) cpu.exec() print(f'Part 1 output:') try: while True: print(str(cpu.get_output(False)) + ' ', ) except Halted: print('done') ### Part 2 memory = numpy.array(memory_, copy=True) cpu = CPU(memory) cpu.queue_input(2) cpu.exec() print(f'Part 2 output:') try: while True: print(str(cpu.get_output(False)) + ' ', ) except Halted: print('done')
def main(): # part 1 memory_ = numpy.loadtxt(r'day17\input.txt', delimiter=',', dtype=numpy.int64) memory = numpy.array(memory_, copy=True) cpu = CPU(memory) cpu.background_exec() grid = defaultdict(lambda: ord('.')) (x, y) = (0, 0) bot_loc = (0, 0) while True: try: val = cpu.get_output(block=True) if val == 10: # new row x = 0 y += 1 #print_grid(grid) continue if val == ord('^'): bot_loc = (x, y) grid[(x, y)] = val x += 1 except Halted: print('Program Halted') break print(f'Part 1: {sum( x*y for (x,y) in find_intersections(grid))}') ### part 2: sub_a = 'R,8,L,4,R,4,R,10,R,8\n' sub_b = 'L,12,L,12,R,8,R,8\n' sub_c = 'R,10,R,4,R,4\n' main_routine = 'A,A,B,C,B,C,B,C,C,A\n' memory = numpy.array(memory_, copy=True) memory[0] = 2 # set to override mode cpu = CPU(memory) # queue inputs for c in main_routine + sub_a + sub_b + sub_c + 'n\n': cpu.queue_input(ord(c)) # run cpu.exec() while True: try: space_dust = cpu.get_output() except (Empty, Halted): break print(f'Part 2: {space_dust}')
def main(): # part 1 memory_ = numpy.loadtxt('day13\input.txt', delimiter=',', dtype=numpy.int64) memory = numpy.array(memory_, copy=True) cpu = CPU(memory) cpu.background_exec() screen = defaultdict(lambda: 0) while True: try: x = cpu.get_output(block=True) y = cpu.get_output(block=True) val = cpu.get_output(block=True) screen[(x, y)] = val print(f'({x}, {y}) {val}') except Halted: print('halted') break blocks = len([i for i in screen.values() if i == BLOCK]) print(f'Part 1: {blocks}') memory = numpy.array(memory_, copy=True) memory[0] = 2 # hack to make paddle the whole width memory[1608:1652] = 3 cpu = CPU(memory) for i in range(10000): cpu.queue_input(0) screen = defaultdict(lambda: 0) cpu.background_exec() score = 0 while True: try: x = cpu.get_output(block=True) y = cpu.get_output(block=True) val = cpu.get_output(block=True) if (x == -1) and (y == 0): score = val #screen[(x,y)] = val #print(f'({x}, {y}) {val}') except Halted: print('halted') break print(f'Part 2: {score}')
def main(): # part 1 memory_ = numpy.loadtxt('day11\input.txt', delimiter=',', dtype=numpy.int64) memory = numpy.array(memory_, copy=True) cpu = CPU(memory) cpu.background_exec() (x, y) = (0, 0) panels = defaultdict(lambda: BLACK) facing = up loop_count = 0 panels[(x, y)] = BLACK while (not cpu.done): # and (len(panels) < 400): #print(f'input: {panels[(x,y)]}') loop_count += 1 if not loop_count % 1000: print(loop_count) try: cpu.queue_input(panels[(x, y)]) color = cpu.get_output(block=True) #print(f'{color}') direction = cpu.get_output(block=True) #print(f'{direction}') except Halted: print('Program done') break panels[(x, y)] = color # rotate facing = facing.rotate(direction) # move forward if facing is up: y -= 1 elif facing is right: x += 1 elif facing is down: y += 1 elif facing is left: x -= 1 #print(f'Part 1: {len(panels)}') min_x = min(panels, key=lambda p: p[0])[0] max_x = max(panels, key=lambda p: p[0])[0] min_y = min(panels, key=lambda p: p[1])[1] max_y = max(panels, key=lambda p: p[1])[1] img = numpy.zeros((max_y + 1 - min_y, max_x + 1 - min_x), dtype=numpy.uint8) for (panel, color) in panels.items(): img[panel[1], panel[0]] = color #plt.imshow(img) #plt.show() print(panels)
def main(): # part 1 print('Part 1:') memory_ = numpy.loadtxt('day5\input.txt', delimiter=',', dtype=numpy.int64) memory = numpy.array(memory_, copy=True) cpu = CPU(memory) cpu.queue_input(1) cpu.exec() # part 2 print('=============================================\nPart 2') memory_ = numpy.loadtxt('day5\input.txt', delimiter=',', dtype=numpy.int64) memory = numpy.array(memory_, copy=True) #test = [3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99] #memory = numpy.array(test, dtype=numpy.int64) cpu = CPU(memory) cpu.queue_input(5) cpu.exec()
def main(): # part 1 memory_ = numpy.loadtxt('day15\input.txt', delimiter=',', dtype=numpy.int64) memory = numpy.array(memory_, copy=True) cpu = CPU(memory) cpu.background_exec() maze = defaultdict(int) (x, y) = (0, 0) facing = N left = None right = None fwd = None maze[(x, y)] = EMPTY root_node = MazeNode() root_thread = threading.Thread(target=maze_worker, args=(cpu, facing, maze, root_node, (x, y))) root_thread.start() first = True if do_plot: while True: task = plot_q.get() if task: args, kwargs = task plt.imshow(*args, **kwargs) #plt.pause(0.0001) if first: plt.show(block=False) first == False else: plt.draw() time.sleep(0) else: break print('plot kill') root_thread.join() test = target_node steps = 0 while test: steps += test.length test = test.parent print(f'part 1: {steps}') plot(maze, 0, 0, show=True) print(target_node) ### part 2 print(f'Part 2: {fill_up(target_node)}')
def y2021_d7(): print('2021 Day 7 Easter Egg: run input as intcode program') memory_ = numpy.loadtxt('y2021\day7\input.txt', delimiter=',', dtype=numpy.int64) memory = numpy.array(memory_, copy=True) cpu = CPU(memory) cpu.background_exec() try: while True: print(chr(cpu.get_output(block=True)), end='') except Halted: pass print('\nDone')
def main(): # part 1 memory_ = numpy.loadtxt(r'day19\input_mine.txt', delimiter=',', dtype=numpy.int64) # darkgray answer: 379 981 memory = numpy.array(memory_, copy=True) cpu = CPU(memory) grid = defaultdict(int) for x in range(100): for y in range(100): grid[(x,y)] = get_point(cpu, x, y) #print(f'{(x, y)}: {val}') print_grid(grid) count = sum(v for v in grid.values()) print(f'Part 1: {count}') ##### part 2 yprime = 99 xprime = 99 x = 0 y = 0 bl = 0 tr = 0 while True: bl = get_point(cpu, x, y+yprime) tr = get_point(cpu, x+xprime, y) if bl and tr: print(f'Part 2: {(x, y)}') break # iterate x while bl == 0: x += 1 bl = get_point(cpu, x, y+yprime) # iterate y while tr == 0: y += 1 tr = get_point(cpu, x+xprime, y)
def main(): # part 1 memory_ = numpy.loadtxt('day7\input.txt', delimiter=',', dtype=numpy.int64) ##memory_ = numpy.fromstring('3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0', sep=',', dtype=numpy.int64) ##memory_ = numpy.fromstring('3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0', sep=',', dtype=numpy.int64) max_out = None max_seq = None for phase_seq in itertools.permutations(range(5), 5): last_output = 0 for phase in phase_seq: memory = numpy.array(memory_, copy=True) cpu = CPU(memory) cpu.queue_input(phase) cpu.queue_input(last_output) cpu.exec() last_output = cpu.get_output() if not max_out or (last_output > max_out): max_out = last_output max_seq = phase_seq print(f'Part 1: Max: {max_out}') print(f'Part 1: Max Seq: {max_seq}') # 3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5 # part 2 #memory_ = numpy.fromstring('3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5', sep=',', dtype=numpy.int64) max_out = None max_seq = None for phase_seq in itertools.permutations(range(5, 10), 5): cpu_a = CPU(numpy.array(memory_, copy=True), input_block=True) cpu_b = CPU(numpy.array(memory_, copy=True), input_block=True, input_q=cpu_a.output_q) cpu_c = CPU(numpy.array(memory_, copy=True), input_block=True, input_q=cpu_b.output_q) cpu_d = CPU(numpy.array(memory_, copy=True), input_block=True, input_q=cpu_c.output_q) cpu_e = CPU(numpy.array(memory_, copy=True), input_block=True, input_q=cpu_d.output_q) cpu_a.input_q = cpu_e.output_q cpu_a.queue_input(phase_seq[0]) cpu_a.queue_input(0) cpu_b.queue_input(phase_seq[1]) cpu_c.queue_input(phase_seq[2]) cpu_d.queue_input(phase_seq[3]) cpu_e.queue_input(phase_seq[4]) # start CPU's in threads a_thread = threading.Thread(target=cpu_a.exec) b_thread = threading.Thread(target=cpu_b.exec) c_thread = threading.Thread(target=cpu_c.exec) d_thread = threading.Thread(target=cpu_d.exec) e_thread = threading.Thread(target=cpu_e.exec) a_thread.start() b_thread.start() c_thread.start() d_thread.start() e_thread.start() e_thread.join() last_output = cpu_e.get_output() if not max_out or (last_output > max_out): max_out = last_output max_seq = phase_seq print(f'Part 2 max out: {max_out}')