示例#1
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
示例#2
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)
示例#3
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()))
示例#4
0
def do_the_thing(content):
    raw_program = list(map(lambda x: int(x), content.split(',')))

    total_affected = 0
    for y in range(50):
        for x in range(50):
            p = IntCode(raw_program)
            p.inputs = [x, y]
            out, done = p.run_io([x, y])
            total_affected += out

    return total_affected
示例#5
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
示例#6
0
def find_max_output(string_program):
    raw_program = list(map(lambda x: int(x), string_program.split(',')))

    max_output = 0
    perms = itertools.permutations(range(5))
    for perm in perms:
        output = 0
        for phase in perm:
            p = IntCode(raw_program, [phase])
            output, _ = p.run_io(output)

        max_output = max(output, max_output)

    return max_output
示例#7
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])
示例#8
0
def solve(data):
    best = MaxIO()

    for phases in permutations(range(5)):
        io = LastIO()
        for phase in phases:
            IntCode(data, IOEventRelay([phase, io.value], best, io)).run()

    print(best.max)
示例#9
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
示例#10
0
def do_the_thing(content):
    raw_program = list(map(lambda x: int(x), content.split(',')))
    raw_program[0] = 2
    p = IntCode(raw_program)
    main_program = "A,B,A,C,B,A,C,B,A,C\n"
    function_A = "L,6,L,4,R,12\n"
    function_B = "L,6,R,12,R,12,L,8\n"
    function_C = "L,6,L,10,L,10,L,6\n"
    print_output = "n\n"
    p.inputs += [ord(c) for c in list(main_program)]
    p.inputs += [ord(c) for c in list(function_A)]
    p.inputs += [ord(c) for c in list(function_B)]
    p.inputs += [ord(c) for c in list(function_C)]
    p.inputs += [ord(c) for c in list(print_output)]
    p.run_out()

    render_image(p.outputs)

    return p.outputs[-1]
示例#11
0
def find_beam_boundary(program, y):
    x_max = 10000
    x_lb = 0
    x_ub = x_max
    x_search_step = math.floor(y / 20)
    x_search = 0
    while True:
        # print('x_search', x_search)
        if x_search > x_max:
            break
        p = IntCode(program)
        out, _ = p.run_io([x_search, y])
        if out:
            x_lb = x_search
        if x_lb and not out:
            x_ub = x_search
            break
        x_search += x_search_step

    return x_lb, x_ub
示例#12
0
def do_the_thing(content):
    raw_program = list(map(lambda x: int(x), content.split(',')))
    p = IntCode(raw_program)
    p.run_out()

    render_image(p.outputs)
    positions = convert_image_to_pos_data(p.outputs)

    alignment_parameters = []
    for position, char in positions.items():
        top_char = get_char_code(positions, position, -1, 0)
        bottom_char = get_char_code(positions, position, 1, 0)
        left_char = get_char_code(positions, position, 0, -1)
        right_char = get_char_code(positions, position, 0, 1)
        if top_char == bottom_char == left_char == right_char == char == 35:
            print(position)
            alignment_parameter = position[0] * position[1]
            alignment_parameters.append(alignment_parameter)

    return sum(alignment_parameters)
示例#13
0
def solve(data):
    best = MaxIO()

    for phases in permutations(range(5, 10)):
        io = MaxIO([0])
        computers = [IntCode(data, IOEventRelay([phase], io, best)) for phase in phases]

        while not all(cpu.terminated for cpu in computers):
            for cpu in filter(lambda c: not c.terminated, computers):
                cpu.resume(io.first())

    return best.max
示例#14
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)
示例#15
0
def find_max_output(string_program):
    raw_program = list(map(lambda x: int(x), string_program.split(',')))

    max_output = 0
    perms = itertools.permutations(range(5, 10))
    for perm in perms:
        input = 0
        programs = []
        for ix, phase in enumerate(perm):
            p = IntCode(raw_program, [phase])
            input, _ = p.run_io(input)
            programs.append(p)

        done = False
        while not done:
            for ix, phase in enumerate(perm):
                input, done = programs[ix].run_io(input)

        round_output = input
        print(round_output)

        max_output = max(round_output, max_output)

    return max_output
示例#16
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)
示例#17
0
def find_change_along_y(program, x, y_lb, y_ub):
    p = IntCode(program)
    lb_value, _ = p.run_io([x, y_lb])

    while y_ub - y_lb > 1:
        y_search = math.floor((y_ub + y_lb) / 2)
        p = IntCode(program)
        out, _ = p.run_io([x, y_search])
        # print('y_search', y_search, out)
        if out == lb_value:
            y_lb = y_search
        else:
            y_ub = y_search

    return y_lb, y_ub
示例#18
0
def find_change_along_x(program, x_lb, x_ub, y):
    p = IntCode(program)
    lb_value, _ = p.run_io([x_lb, y])

    while x_ub - x_lb > 1:
        x_search = math.floor((x_ub + x_lb) / 2)
        p = IntCode(program)
        out, _ = p.run_io([x_search, y])
        # print('x_search', x_search, out)
        if out == lb_value:
            x_lb = x_search
        else:
            x_ub = x_search

    return x_lb, x_ub
示例#19
0
def solve_part_1(data):
    data[1], data[2] = 12, 2
    print(IntCode(data).run().memory[0])
示例#20
0
def solve(program):
    IntCode(program, IO.from_args(2)).run()
示例#21
0
def test_intcode_day9_1():
    assert IntCode([109, -1, 4, 1, 99]).run().output == -1
示例#22
0
def test_intcode_day9_8():
    IN = 1337
    # print()
    assert IntCode([109, 1, 203, 2, 204, 2, 99], IO.from_args(IN),
                   debug=True).run().output == IN
示例#23
0
def test_intcode_day9_7():
    IN = 1336
    assert IntCode([109, 1, 3, 3, 204, 2, 99],
                   IO.from_args(IN)).run().output == IN
示例#24
0
def test_intcode_day9_6():
    assert IntCode([109, 1, 209, -1, 204, -106, 99]).run().output == 204
示例#25
0
def test_intcode_day9_3():
    assert IntCode([109, -1, 204, 1, 99]).run().output == 109
示例#26
0
import sys

sys.path.append('..')
from shared.intcode import IntCode

with open('input.txt', 'r') as f:
    for line in f:
        content = line.strip('\n')
        program = list(map(lambda x: int(x), content.split(',')))
        dict_program = dict(x for x in enumerate(program))

p = IntCode(program)
output, _ = p.run_io(5)
print(output)
示例#27
0
def solve_part_1(data):
    IntCode(data, IO([5])).run()
示例#28
0
def test_intcode_day9_5():
    assert IntCode([109, 1, 109, 9, 204, -6, 99]).run().output == 204
示例#29
0
def run_program(string_program, input):
    raw_program = list(map(lambda x: int(x), string_program.split(',')))
    p = IntCode(raw_program)
    return p.run_out(input)
示例#30
0
 def __init__(self, program, address, queue):
     self.p = IntCode(program, [address])
     self.input_queue = []
     self.queue = queue