def part1(): machine = Machine([j for j in inp]) while True: try: machine.run_machine([1]) except Output as e: print (e.output) except Interrupt: break
def get_field(program: [int] = program) -> [[Object]]: machine = Machine(program) output = machine.run() field = [[]] for o in output: s = chr(o) if s == '\n': field.append([]) else: field[-1].append(Object(s)) return list(filter(bool, field))
def main(program=program) -> int: machine = Machine(program) result = machine.run() screen = {} i = 0 while i < len(result): x = result[i] y = result[i + 1] tile = Tile(result[i + 2]) screen[(x, y)] = tile i += 3 return sum(tile == Tile.BLOCK for tile in screen.values())
def get_painted_panels(input: [int], start_color: Color = Color.BLACK): panels = {(0, 0): start_color} # All panels are black, so is the starting panel head = Head(Direction.UP, 0, 0) q_in = Queue() q_out = Queue() machine = Machine(input, [start_color.value], q_in, q_out) t = Thread(target=machine.run) t.start() while t.is_alive(): color_to_paint = q_out.get() panels[(head.x, head.y)] = Color(color_to_paint) turn = Turn(q_out.get()) head.move(turn) color: Color if (head.x, head.y) in panels: color = panels[(head.x, head.y)] else: color = Color.BLACK q_in.put(color.value) return panels
def part2(): permutations = list(itertools.permutations(range(5, 10))) output_signals = [] for permutation in permutations: machines = [Machine([j for j in inp]) for _ in range(5)] next_signal = 0 i = 0 phase_flag = False while True: try: if not phase_flag: machines[i].run_machine([permutation[i], next_signal]) else: machines[i].run_machine([next_signal]) except Output as e: next_signal = e.output except Interrupt: break i = (i + 1) % 5 if i == 0: phase_flag = True output_signals.append(next_signal) return max(output_signals)
def part1(): permutations = list(itertools.permutations(range(5))) output_signals = [] for permutation in permutations: next_signal = 0 for phase in permutation: while True: try: machine = Machine([j for j in inp]) machine.run_machine([phase, next_signal]) except Output as e: next_signal = e.output break output_signals.append(next_signal) return max(output_signals)
def part1(): grid = {} machine = Machine([j for j in inp]) robot = (0, 0) robot_direction = 0 color = 0 output_type = 0 directions = {0: (0, 1), 1: (1, 0), 2: (0, -1), 3: (-1, 0)} while True: try: if output_type == 0: machine.run_machine([color]) else: machine.run_machine([]) except Output as e: if output_type == 0: grid[robot] = e.output output_type = 1 elif output_type == 1: if e.output == 0: robot_direction = (robot_direction - 1) % 4 if e.output == 1: robot_direction = (robot_direction + 1) % 4 robot = (robot[0] + directions[robot_direction][0], robot[1] + directions[robot_direction][1]) if robot in grid: color = grid[robot] else: color = 0 output_type = 0 except Interrupt: break return len(grid.keys())
def part2(): grid = {} machine = Machine([j for j in inp]) robot = (0, 0) robot_direction = 0 color = 1 output_type = 0 directions = {0: (0, 1), 1: (1, 0), 2: (0, -1), 3: (-1, 0)} while True: try: if output_type == 0: machine.run_machine([color]) else: machine.run_machine([]) except Output as e: if output_type == 0: grid[robot] = e.output output_type = 1 elif output_type == 1: if e.output == 0: robot_direction = (robot_direction - 1) % 4 if e.output == 1: robot_direction = (robot_direction + 1) % 4 robot = (robot[0] + directions[robot_direction][0], robot[1] + directions[robot_direction][1]) if robot in grid: color = grid[robot] else: color = 0 output_type = 0 except Interrupt: break miny = 0 minx = 0 maxy = 0 maxx = 0 for cord in grid: if cord[0] < minx: minx = cord[0] if cord[0] > maxx: maxx = cord[0] if cord[1] < miny: miny = cord[1] if cord[1] > maxy: maxy = cord[1] painted = [[' ' for _ in range(maxx - minx + 1)] for _ in range(maxy - miny + 1)] for cord in grid: painted[cord[1] - miny][cord[0] - minx] = '█' if grid[cord] == 1 else ' ' painted.reverse() for row in painted: print(''.join(row))
def part1(noun, verb): machine = Machine([j for j in inp]) machine.code[1] = noun machine.code[2] = verb try: machine.run_machine([]) except Interrupt: pass return machine.code[0]
def play(program=program, ai_mode=False, with_display=True) -> int: program[0] = 2 q_in = Queue() q_out = Queue() machine = Machine(program, [], q_in, q_out) t = Thread(target=machine.run) t.start() screen = {} score = 0 while t.is_alive(): score = update_screen(screen, q_out) or score while not q_out.empty(): score = update_screen(screen, q_out) or score if with_display: draw_screen(screen) q_in.put(ai_input(screen) if ai_mode else joystick_input()) return score
def explore_field(program=program): field = {(0, 0): Object.EMPTY} # The first field is always empty pos = (0, 0) direction = Direction.EAST o = Object.EMPTY travaled = {} q_in = Queue() q_out = Queue() machine = Machine(program, [], q_in, q_out) t = Thread(target=machine.run) t.start() count = 0 try: while True: s = field_str(field, pos) #print(s) print(s.count('?')) direction, o = explore(field, pos, q_in, q_out, travaled) travaled[pos] = direction if o == Object.WALL: field[move_in_direction_from(pos, direction)] = o else: # Position has been changed pos = move_in_direction_from(pos, direction) field[pos] = o except (KeyboardInterrupt, SystemExit): pass print("WRITING TO FILE...") with open(FILE_NAME, 'wb') as file: pickle.dump(field, file) print("WRITTEN TO FILE")
def main(program: [int] = program): field = {(0, 0): Object.EMPTY} # The first field is always empty pos = (0, 0) direction = Direction.EAST o = Object.EMPTY travaled = {} q_in = Queue() q_out = Queue() machine = Machine(program, [], q_in, q_out) t = Thread(target=machine.run) t.start() while True: print_field(field, pos) direction, o = explore(field, pos, q_in, q_out, travaled) travaled[pos] = direction if o == Object.WALL: field[move_in_direction_from(pos, direction)] = o else: # Position has been changed pos = move_in_direction_from(pos, direction) field[pos] = o
from intcode import Machine # Open the file and turn it into a list of ints with open('input.txt', 'r') as input_file: input_program = input_file.readline() # Question 1 q1_machine = Machine(input_program, param_ranges=[(1, [12]), (2, [2])]) q1_output, _ = q1_machine.run_one() q1_out_state, _, _ = q1_output print(f'Q1 answer: {q1_out_state[0]}') # Question 2 # q2_machine = Machine(input_program, param_ranges=[(1,[89]),(2,[76])]) q2_machine = Machine(input_program, param_ranges=[(1, range(0, 100)), (2, range(0, 100))]) q2_output, q2_params = q2_machine.run_till_predicate(lambda x: x == 19690720) print(f'Q2 answers: {q2_params}')
painted = 0 if not position in hull: painted = 1 hull[position] = colour return painted if __name__ == '__main__': # unittest.main() program_source = "3,8,1005,8,325,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,0,10,4,10,102,1,8,29,1006,0,41,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,1001,8,0,54,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,102,1,8,76,1,9,11,10,2,5,2,10,2,1107,19,10,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,0,10,4,10,101,0,8,110,2,1007,10,10,2,1103,13,10,1006,0,34,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,102,1,8,142,1006,0,32,1,101,0,10,2,9,5,10,1006,0,50,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,101,0,8,179,1,1005,11,10,2,1108,11,10,1006,0,10,1,1004,3,10,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,1,10,4,10,1002,8,1,216,1,1002,12,10,2,1102,3,10,1,1007,4,10,2,101,7,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,0,8,10,4,10,102,1,8,253,2,104,3,10,1006,0,70,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,102,1,8,282,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,101,0,8,305,101,1,9,9,1007,9,962,10,1005,10,15,99,109,647,104,0,104,1,21102,838211572492,1,1,21102,342,1,0,1105,1,446,21102,825326674840,1,1,21101,0,353,0,1106,0,446,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21101,0,29086686211,1,21102,1,400,0,1106,0,446,21102,209420786919,1,1,21101,0,411,0,1105,1,446,3,10,104,0,104,0,3,10,104,0,104,0,21101,0,838337298792,1,21101,434,0,0,1105,1,446,21101,988661154660,0,1,21102,1,445,0,1106,0,446,99,109,2,21201,-1,0,1,21101,40,0,2,21101,0,477,3,21101,0,467,0,1105,1,510,109,-2,2106,0,0,0,1,0,0,1,109,2,3,10,204,-1,1001,472,473,488,4,0,1001,472,1,472,108,4,472,10,1006,10,504,1101,0,0,472,109,-2,2106,0,0,0,109,4,1201,-1,0,509,1207,-3,0,10,1006,10,527,21102,0,1,-3,22102,1,-3,1,22102,1,-2,2,21101,0,1,3,21101,546,0,0,1105,1,551,109,-4,2105,1,0,109,5,1207,-3,1,10,1006,10,574,2207,-4,-2,10,1006,10,574,21201,-4,0,-4,1105,1,642,21201,-4,0,1,21201,-3,-1,2,21202,-2,2,3,21102,1,593,0,1105,1,551,21202,1,1,-4,21102,1,1,-1,2207,-4,-2,10,1006,10,612,21102,0,1,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,634,21202,-1,1,1,21102,1,634,0,105,1,509,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2106,0,0" program = [int(s) for s in program_source.split(",")] current_position = (0, 0) current_direction = NORTH hull = {} painted = 0 machine = Machine() machine.memory = program.copy() machine.counter = 0 machine.io = {'input': [0], 'output': []} run = True while (run): while (machine.execute_instruction() and len(machine.io['output']) != 2): pass if len(machine.io['output']) != 2: run = False else: debug(machine.io) colour = machine.io['output'][0] rotation = machine.io['output'][1] painted = painted + paint_panel(current_position, colour, hull)
import sys import unittest from intcode import Machine # Steps to increment for each code def debug(arg): #pass print(arg) if __name__=='__main__': # unittest.main() program = [int(s) for s in sys.stdin.readline().strip().split(",")] machine = Machine() machine.memory = program.copy() machine.counter = 0 machine.io = {'input': [2], 'output': []} while (machine.execute_instruction()): pass print(machine.io)
from intcode import Machine # Open the file and turn it into a list of ints with open('input.txt', 'r') as input_file: input_program = input_file.readline() # Question 1 q1_machine = Machine(input_program) q1_output, _ = q1_machine.run_one(inputs=[1]) q1_state, _, q1_output_buf = q1_output print(q1_output_buf) # Question 2 q2_machine = Machine(input_program) q2_output, _ = q2_machine.run_one(inputs=[5]) q2_state, _, q2_output_buf = q2_output print(q2_output_buf)