Esempio n. 1
0
def program_springbot(content):
    raw_program = list(map(lambda x: int(x), content.split(',')))

    p = IntCode(raw_program)
    done = p.run(True)
    print('done?', done)
    render(p.outputs)

    instructions = [
        "NOT A T",
        "NOT B J",
        "OR T J",
        "NOT C T",
        "OR T J",
        "AND D J",
        "WALK"
    ]
    instructions_encoded = []
    for instruction in instructions:
        instructions_encoded += [ord(c) for c in instruction+"\n"]

    p.inputs = instructions_encoded
    done = p.run(True)
    print('done?', done)
    return render(p.outputs)
Esempio n. 2
0
class NetworkComputers:
    def __init__(self, program, address, queue):
        self.p = IntCode(program, [address])
        self.input_queue = []
        self.queue = queue

    def run(self):
        if len(self.input_queue):
            self.p.inputs += self.input_queue
            self.input_queue = []
        else:
            self.p.inputs += [-1]

        # print('inputs', self.p.inputs)
        done = self.p.run(True)
        if self.p.outputs:
            # print('outputs', self.p.outputs)
            self.queue.extend(self.p.outputs)
            self.p.outputs = []

        return done

    def receive(self, packet):
        self.input_queue.append(packet[0])
        self.input_queue.append(packet[1])
Esempio n. 3
0
def solve_part_1(data):
    for a in range(100):
        for b in range(100):
            cpu = IntCode(data)
            cpu.memory.program[1:3] = a, b
            if cpu.run().memory[0] == 19690720:
                return 100 * a + b
Esempio n. 4
0
def program_springbot(content):
    raw_program = list(map(lambda x: int(x), content.split(',')))

    p = IntCode(raw_program)
    p.run(True)

    instructions = [
        "NOT T T", "AND A T", "AND B T", "AND C T", "NOT T T", "AND D T",
        "OR E J", "OR H J", "AND T J", "RUN"
    ]

    instructions_encoded = []
    for instruction in instructions:
        instructions_encoded += [ord(c) for c in instruction + "\n"]

    p.inputs = instructions_encoded
    p.run(True)
    return render(p.outputs)
Esempio n. 5
0
def do_the_thing(content):
    raw_program = list(map(lambda x: int(x), content.split(',')))

    p = IntCode(raw_program)

    positions = dict()
    position = (0, 0)
    direction = 1
    positions[position] = (1, 0, 10000)
    done = False
    count = 0
    breaker = 3000000
    n_steps_so_far = 0
    o_found = False
    n_steps_from_o = -1
    while not done:
        # print(position)
        p.inputs.append(direction)
        done = p.run(True)
        status = p.outputs.pop(0)

        if status == 0:
            positions[next_position(position, direction)] = (0, 0, 0)
            direction = randint(1, 4)
        else:
            position = next_position(position, direction)
            n_steps_so_far = n_steps_so_far + 1
            if position in positions:
                n_steps_so_far = min(n_steps_so_far, positions[position][1])
            if o_found:
                n_steps_from_o = n_steps_from_o + 1
                if position in positions and positions[position][2] > -1:
                    n_steps_from_o = min(n_steps_from_o,
                                         positions[position][2])
            positions[position] = (status, n_steps_so_far, n_steps_from_o)
            direction = randint(1, 4)

        if status == 2:
            print("found it!")
            print(position)
            print(n_steps_so_far)
            o_found = True
            n_steps_from_o = 0

        print(position)
        print(count, breaker, n_steps_so_far, n_steps_from_o,
              max(v[2] for k, v in positions.items()))
        if count > breaker:
            break

        count += 1

    display(positions)
    print(max(v[2] for k, v in positions.items()))
Esempio n. 6
0
def do_the_thing(content):
    raw_program = list(map(lambda x: int(x), content.split(',')))
    raw_program[0] = 2

    p = IntCode(raw_program)
    p.inputs = []

    done = False
    while not done:
        done = p.run(True)
        # if not done:
        #     print('Still Playing...')
        instructions = p.outputs

        n_blocks = 0
        # screen = np.full([20, 50], ' ', dtype=object)

        current_score = 0
        ball_pos = 0
        paddle_pos = 0
        ix = 0
        while ix < len(instructions):
            ic = instructions[ix]
            ir = instructions[ix + 1]
            tile_id = instructions[ix + 2]
            if tile_id == 2:
                n_blocks += 1
            ix += 3
            if tile_id == 4:
                ball_pos = ic
            if tile_id == 3:
                paddle_pos = ic

            if ic == -1 and ir == 0:
                current_score = tile_id
            # else:
            # screen[ir, ic] = paint(tile_id)

        # draw(screen)
        # print(current_score)

        if not done:
            joystick_command = 0
            if ball_pos > paddle_pos:
                joystick_command = 1
            elif ball_pos < paddle_pos:
                joystick_command = -1
            p.inputs.append(joystick_command)

    return current_score
Esempio n. 7
0
def do_the_thing(content):
    raw_program = list(map(lambda x: int(x), content.split(',')))

    p = IntCode(raw_program)

    p.inputs = []
    _ = p.run()
    instructions = p.outputs

    n_blocks = 0
    ix = 0
    while ix < len(instructions):
        tile_id = instructions[ix + 2]
        if tile_id == 2:
            n_blocks += 1
        ix += 3

    return n_blocks
Esempio n. 8
0
def do_the_thing(content):
    raw_program = list(map(lambda x: int(x), content.split(',')))
    p = IntCode(raw_program)

    grid = dict()
    position = (0, 0)
    grid[position] = 1
    facing = 0
    done = False
    while not done:
        see = grid[position] if position in grid else 0

        p.inputs = [see]
        done = p.run(True)

        color = p.outputs.pop(0)
        turn_direction = p.outputs.pop(0)

        # Paint
        grid[position] = color

        # Turn
        turn = -1 if turn_direction == 0 else 1
        facing = (facing + turn) % 4

        # Move
        if facing == 0:
            position = (position[0], position[1]-1)
        elif facing == 1:
            position = (position[0]+1, position[1])
        elif facing == 2:
            position = (position[0], position[1]+1)
        elif facing == 3:
            position = (position[0]-1, position[1])

        if done:
            print('done')

    paint(grid)