Пример #1
0
def find_box(program, box_size):
    left = right = 0
    ends = []
    y = 0
    while True:
        left_val = 0
        while not left_val:
            left_val, _, _ = execute(program[:], [left, y])
            if not left_val:
                left += 1
        right_val = 1
        if y < 5:
            # Special case deals with 1 width beam moving >1 space between rows
            seen_beam = False
            x = 0
            for x in range(10):
                right_val, _, _ = execute(program[:], [x, y])
                if right_val:
                    seen_beam = True
                elif seen_beam:
                    break
            right = x
        else:
            while right_val:
                right_val, _, _ = execute(program[:], [right, y])
                if right_val:
                    right += 1
        ends.append(right)
        if (
            right - left >= box_size and
                ends[y + 1 - box_size] > left + box_size - 1
        ):
            return left * 10000 + y + 1 - box_size
        y += 1
Пример #2
0
def count_pulls(program):
    count = 0
    left = right = 0
    for y in range(50):
        left_val = 0
        while not left_val:
            left_val, _, _ = execute(program[:], [left, y])
            if not left_val:
                left += 1
        right_val = 1
        if y < 5:
            # Special case deals with 1 width beam moving >1 space between rows
            seen_beam = False
            x = 0
            for x in range(10):
                right_val, _, _ = execute(program[:], [x, y])
                if right_val:
                    seen_beam = True
                elif seen_beam:
                    break
            right = x
        else:
            while right_val:
                right_val, _, _ = execute(program[:], [right, y])
                if right_val:
                    right += 1
        row_len = min(right, 50) - min(left, 50)
        count += row_len
    return count
Пример #3
0
 def instance(idx, ingress):
     program_instance = program[:]
     ingress.put(idx)
     provider = input_provider(ingress)
     pc = rb = 0
     while True:
         d, pc, rb = execute(program_instance, provider, pc, rb)
         x, pc, rb = execute(program_instance, provider, pc, rb)
         y, pc, rb = execute(program_instance, provider, pc, rb)
         if SHOULD_STOP:
             return
         egress.put((d, (x, y)))
Пример #4
0
def play_game(program):
    val = pc = rb = 0
    screen = np.zeros((38, 22), dtype=np.int8)
    output = []
    last_score = 0
    while val != "HALT":
        ball_pos = np.argwhere(screen == 4)
        paddle_pos = np.argwhere(screen == 3)
        if ball_pos.shape[0] == 0 or paddle_pos.shape[0] == 0:
            direction = 0
        else:
            ball_x = ball_pos[0][0]
            paddle_x = paddle_pos[0][0]
            if ball_x == paddle_x:
                direction = 0
            elif ball_x < paddle_x:
                direction = -1
            else:
                direction = 1
        val, pc, rb = execute(program, [direction], pc, rb)
        output.append(val)
        if len(output) == 3:
            if output[0] == -1 and output[1] == 0:
                last_score = output[2]
            screen[output[0], output[1]] = output[2]
            output = []
    print(last_score)
Пример #5
0
def rescue_bots(program, instructions):
    program[0] = 2
    cmds = iter([ord(c) for p in instructions for c in p + "\n"])
    idx = pc = rb = out = 0
    while out < 128:
        idx += 1
        out, pc, rb = execute(program, cmds, pc, rb)
    return out
Пример #6
0
def calc_blocks(program):
    val = pc = rb = 0
    screen = np.zeros((38, 22), dtype=np.int8)
    output = []
    while val != "HALT":
        val, pc, rb = execute(program, [], pc, rb)
        output.append(val)
        if len(output) == 3:
            screen[output[0], output[1]] = output[2]
            output = []
    return np.sum(screen == 2)
Пример #7
0
def run(program):
    out = pc = rb = 0
    cmds = generate_cmds()
    while out < 128:
        out, pc, rb = execute(program, cmds, pc, rb)
        if out == "HALT":
            return
        elif out >= 128:
            print(f"Damage: {out}")
            return
        elif out == 10:
            print()
        else:
            print(chr(out), end="")
Пример #8
0
def render_grid(program):
    pc = rb = 0
    rows = []
    row = []
    while True:
        out, pc, rb = execute(program, [], pc, rb)
        if out == "HALT":
            if row:
                print("Adding final row")
                rows.append(row)
            break
        elif out == 10:
            if row:
                rows.append(row)
                row = []
        else:
            row.append(chr(out))
    return np.array(rows).transpose((1, 0))
Пример #9
0
def create_map(program):
    def add_candidates(l, pos, state, c, r, cnt):
        for cmd, move in directions.items():
            l.append((pos + move, cmd, state[:], c, r, cnt + 1))

    explored_nodes = defaultdict(lambda: -1)
    oxygen_location = None
    oxygen_steps = None
    explored_nodes[(0, 0)] = 1
    frontier = []
    add_candidates(frontier, np.array([0, 0]), program, 0, 0, 0)
    while len(frontier) > 0:
        new_pos, instr, s, pc, rb, steps = frontier.pop(0)
        if explored_nodes[tuple(new_pos)] != -1:
            continue
        out, pc, rb = execute(s, [instr], pc, rb)
        explored_nodes[tuple(new_pos)] = out
        if out == 2:
            oxygen_location = new_pos
            oxygen_steps = steps
        if out != 0:
            add_candidates(frontier, new_pos, s, pc, rb, steps)
    return oxygen_location, oxygen_steps, explored_nodes
Пример #10
0
        yield 10
    for c in "south":
        yield ord(c)
    yield 10


if __name__ == "__main__":
    with open("../input/day25.txt") as f:
        program_text = f.readline()
    program = [int(val) for val in program_text.split(",")]
    pc = rb = 0
    gen = input_generator(instructions)
    buffer = ""
    checkpoint = False
    while True:
        out, pc, rb = execute(program, gen, pc, rb)
        if out == "HALT":
            break
        buffer += chr(out)
        if out == 10:
            if "Security Checkpoint" in buffer:
                checkpoint = True
            elif checkpoint and "Command" in buffer:
                break
            buffer = ""
    saved_program, saved_pc, saved_rb = program[:], pc, rb
    for num_to_drop in range(len(possible_items) + 1):
        for to_drop in itertools.combinations(possible_items, num_to_drop):
            program, pc, rb = saved_program[:], saved_pc, saved_rb
            gen = drop_generator(to_drop)
            failed = False