def code1(): with open(DATA) as f: strcode = f.readline().strip() code = intcode.parse_data(strcode) comp = intcode.Intcode(code) comp.run([1]) print('1>', comp.outvalues)
def code2(): with open(DATA) as f: strcode = f.readline().strip() code = intcode.parse_data(strcode) maxval = 0 for phase_setting in itertools.permutations(range(5, 10)): maxval = max(maxval, try_loop_phase_setting(code, phase_setting)) print('2>', maxval)
def code2(): with open(DATA) as f: strcode = f.readline().strip() code = intcode.parse_data(strcode) computer = intcode.Intcode(code) computer.verbose_output = False computer.code[0] = 2 tiles = defaultdict(lambda: defaultdict(int)) play(tiles, computer, [0], trace=False)
def code1(): with open(DATA) as f: strcode = f.readline().strip() code = intcode.parse_data(strcode) computer = intcode.Intcode(code) computer.verbose_output = False panels = defaultdict(int) panels = run_robot(panels, computer) print('1>', len(panels))
def code1(): with open(DATA) as f: strcode = f.readline().strip() code = intcode.parse_data(strcode) computer = intcode.Intcode(code) computer.verbose_output = False area = defaultdict(lambda: defaultdict(int)) DFS(area, computer) show_area(area, 0, 0) i, j = find_target(area) BFS(area, 0, 0, i, j) return area, i, j
def runcode(part): with open(DATA) as f: strcode = f.readline().strip() code = intcode.parse_data(strcode) input_stack = [[num] for num in range(50)] NAT_X, NAT_Y = None, None def make_input_callback(num): def input_callback(): return pop_input(input_stack, num) return input_callback input_callback = [make_input_callback(num) for num in range(50)] computers = list() for num in range(50): computer = intcode.Intcode(code) computer.verbose_output = False computer.trace = False computers.append(computer) print(input_stack) while 1: for num in range(50): # print('---', num, input_stack[num]) computers[num].run([], input_callback=input_callback[num], return_output=True, return_input=True) if computers[num].returned_on == 'output': if len(computers[num].outvalues) >= 3: destnum = computers[num].outvalues.pop(0) X = computers[num].outvalues.pop(0) Y = computers[num].outvalues.pop(0) print(num, '-->', destnum, X, Y) if destnum != 255: input_stack[destnum].append(X) input_stack[destnum].append(Y) elif part == 1: print('1>', Y) return else: if all(not _ for _ in input_stack): if Y == NAT_Y: print('2>', Y) return NAT_x, NAT_Y = X, Y input_stack[0].append(X) input_stack[0].append(Y)
def code1(): with open(DATA) as f: strcode = f.readline().strip() code = intcode.parse_data(strcode) computer = intcode.Intcode(code) computer.verbose_output = False computer.trace = False for line in INIT_SEQ.splitlines(): computer.invalues.extend([ord(c) for c in line.strip()] + [10]) items_combinations = list() for n in range(1, len(ITEMS) + 1): items_combinations.extend(list(itertools.combinations(ITEMS, n))) def input_callback(): if computer.outvalues: print(''.join(chr(_) for _ in computer.outvalues)) computer.outvalues.clear() if not computer.invalues: if items_combinations: items = items_combinations.pop(0) test_items(computer, items) else: inp = input() if inp == 'end': exit() if inp == 'n': inp = 'north' if inp == 'e': inp = 'east' if inp == 's': inp = 'south' if inp == 'w': inp = 'west' computer.invalues.extend([ord(c) for c in inp] + [10]) return computer.invalues.pop(0) while 1: computer.run([], input_callback=input_callback, return_output=False, return_input=True) if computer.returned_on == 'terminate': print(''.join(chr(_) for _ in computer.outvalues)) break
def run(springscript): with open(DATA) as f: strcode = f.readline().strip() code = intcode.parse_data(strcode) computer = intcode.Intcode(code) computer.verbose_output = False computer.trace = False code = [line.strip() for line in springscript.splitlines()] codeascii = list() for line in code: codeascii.extend([ord(char) for char in line] + [10]) computer.run(codeascii, return_output=False) s = ''.join([(chr(x) if x < 256 else str(x)) for x in computer.outvalues]) print(s)
def code1(): with open(DATA) as f: strcode = f.readline().strip() code = intcode.parse_data(strcode) computer = intcode.Intcode(code) computer.verbose_output = False tiles = defaultdict(lambda: defaultdict(int)) tiles = draw_tiles(tiles, computer, [], trace=False) n = 0 for row in tiles.values(): n += sum(tile == 2 for tile in row.values()) show_game(tiles) print('1>', n) return tiles, computer
def code1(): with open(DATA) as f: strcode = f.readline().strip() code = intcode.parse_data(strcode) computer = intcode.Intcode(code) computer.verbose_output = False zone = [['.'] * 50 for _ in range(50)] nbeams = 0 for x in range(50): for y in range(50): computer.reset() computer.run([x, y], return_output=True) if computer.outvalues[0] == 1: zone[y][x] = '#' nbeams += 1 for _ in zone: print(''.join(_)) print('1>', nbeams)
def code2(): with open(DATA) as f: strcode = f.readline().strip() code = intcode.parse_data(strcode) computer = intcode.Intcode(code) computer.verbose_output = False panels = defaultdict(int) panels[(0, 0)] = 1 panels = run_robot(panels, computer) print('2>', len(panels)) imin, imax = float('inf'), -float('inf') jmin, jmax = float('inf'), -float('inf') for i, j in panels.keys(): imin = min(imin, i) imax = max(imax, i) jmin = min(jmin, j) jmax = max(jmax, j) print(imin, imax, jmin, jmax) for i in range(imin, imax + 1): s = [panels[(i, j)] for j in range(jmin, jmax + 1)] print(''.join(['.' if _ == 0 else '#' for _ in s]))
def code2(): """ hypoteses: xmin and xmax are increasing and there is no gap between xmin and xmax """ with open(DATA) as f: strcode = f.readline().strip() code = intcode.parse_data(strcode) computer = intcode.Intcode(code) computer.verbose_output = False @functools.lru_cache(maxsize=None) def beam_bounds(xmin, y): for x in itertools.count(xmin): computer.reset() computer.run([x, y], return_output=True) if computer.outvalues[0] == 1: xmin = x break for x in itertools.count(x + 1): computer.reset() computer.run([x, y], return_output=True) if computer.outvalues[0] == 0: xmax = x - 1 break return xmin, xmax y0 = 100 xmin, xmax = beam_bounds(0, y0) for y in itertools.count(y0 + 1): xmin, xmax = beam_bounds(xmin, y) if xmax - xmin + 1 >= 100: computer.reset() computer.run([xmax - 99, y + 99], return_output=True) if computer.outvalues[0] == 1: print('2>', xmax - 99, y, (xmax - 99) * 10000 + y) break
for phase_setting in itertools.permutations(range(5)): maxval = max(maxval, try_phase_setting(code, phase_setting)) print('1>', maxval) def code2(): with open(DATA) as f: strcode = f.readline().strip() code = intcode.parse_data(strcode) maxval = 0 for phase_setting in itertools.permutations(range(5, 10)): maxval = max(maxval, try_loop_phase_setting(code, phase_setting)) print('2>', maxval) code = intcode.parse_data('3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0') result = try_phase_setting(code, (4,3,2,1,0)) assert result == 43210, result code = intcode.parse_data('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') result = try_phase_setting(code, (0,1,2,3,4)) assert result == 54321, result code = intcode.parse_data('3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0') result = try_phase_setting(code, (1,0,4,3,2)) assert result == 65210, result code1() code = intcode.parse_data('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') result = try_loop_phase_setting(code, (9,8,7,6,5))