示例#1
0
文件: 15.py 项目: pshatov/AoC
                sides = get_sideway_directions(d)
                dlt, drt = sides[0], sides[1]
                if dlt in new_can_go_dirs:
                    field.move_in_direction(dlt, level)
                    explore_current_pixel([dlt], level + 1)
                    field.move_in_direction(drt, level)
                if drt in new_can_go_dirs:
                    field.move_in_direction(drt, level)
                    explore_current_pixel([drt], level + 1)
                    field.move_in_direction(dlt, level)

            field.move_in_direction(get_opposite_direction(d), level)
            went_distance -= 1


cpu = Intcode()


def main():

    opcodes = cpu.load_opcodes('input.txt')
    cpu.reset(opcodes)

    init_dirs = []
    for d in MoveDirection:
        init_dirs.append(d)
    explore_current_pixel(init_dirs)

    wave = -1
    route_found = False
    redraw_screen_wave()
示例#2
0
文件: 19.py 项目: pshatov/AoC
def main():

    opcodes = Intcode.load_opcodes('input.txt')

    _vt100_cursor_off()
    _vt100_cursor_xy(0, 0)

    num_points = 0
    for y in range(XY0):

        s = ""
        for x in range(XY0):
            cpu.reset(opcodes)
            cpu.push_input(x)
            cpu.push_input(y)
            cpu.run()
            p = cpu.pop_output()
            if p == 0:
                s += "."
            elif p == 1:
                s += "#"
                num_points += 1

        print(s, flush=True)
        BEAM.append(s)

    xl = s.find("#")
    xr = s.rfind("#")

    for y in range(XY0, Y1):

        found = False
        while not found:
            cpu.reset(opcodes)
            cpu.push_input(xl)
            cpu.push_input(y)
            cpu.run()
            p = cpu.pop_output()
            if p == 0:
                xl += 1
            elif p == 1:
                found = True

        found = False
        while not found:
            cpu.reset(opcodes)
            cpu.push_input(xr)
            cpu.push_input(y)
            cpu.run()
            p = cpu.pop_output()
            if p == 1:
                xr += 1
            elif p == 0:
                xr -= 1
                found = True

        w = xr - xl + 1
        s = "." * xl + "#" * w
        num_points += w

        print(s, flush=True)
        BEAM.append(s)

    print("num_points = %d" % num_points)

    checking = False
    print("\nSkipping too narrow area...", flush=True)
    for y in range(Y1, Y2):

        found = False
        while not found:
            cpu.reset(opcodes)
            cpu.push_input(xl)
            cpu.push_input(y)
            cpu.run()
            p = cpu.pop_output()
            if p == 0:
                xl += 1
            elif p == 1:
                found = True

        found = False
        while not found:
            cpu.reset(opcodes)
            cpu.push_input(xr)
            cpu.push_input(y)
            cpu.run()
            p = cpu.pop_output()
            if p == 1:
                xr += 1
            elif p == 0:
                xr -= 1
                found = True

        w = xr - xl + 1
        s = "." * xl + "#" * w

        BEAM.append(s)

        if not checking:
            if y < 100: continue
            print("Narrow area skipped, searching for solution...\n",
                  flush=True)
            checking = True

        y_dist = 0
        for yy in range(y, y - 100, -1):
            b = BEAM[yy]
            if len(b) < (xl + 1): break
            if b[xl] == "#":
                y_dist += 1
            else:
                break

        x_dist = len(BEAM[y - 99]) - xl

        if (y % 100) == 0:
            print("Scanned %d y-lines..." % y)
            print("  beam x-offset: %d" % xl)
            print("  beam x-width: %d" % w)
            print("  fits y-size: %d " % y_dist)
            print("  fits x-size: %d " % x_dist)
            print("", end='', flush=True)

        if y_dist == 100 and x_dist == 100:
            print("SOLUTION FOUND!")
            print("%d" % (xl * 10000 + (y - 99)))
            break
示例#3
0
def main():

    cpu = Intcode()

    opcodes = cpu.load_opcodes('input.txt')
    cpu.reset(opcodes)

    cpu.run()
    if not cpu.is_stopped(): raise ("Program not stopped.")

    num_blocks = 0
    while cpu.can_pop_output():
        x = cpu.pop_output()
        y = cpu.pop_output()
        t = cpu.pop_output()

        if t == 2: num_blocks += 1

    print("num_blocks = %d" % (num_blocks))

    cpu.reset(opcodes)
    cpu._memory[0] = 2

    while True:

        cpu.run()
        redraw_screen(cpu)
        if cpu.is_stopped(): break

        #key = None
        #while key is None:
        #k = keyboard.read_event(suppress=True)
        #if k.name == 'q' and k.event_type == "down": sys.exit()
        #if k.name == 'z' and k.event_type == "down": key = -1
        #if k.name == 'x' and k.event_type == "down": key = 0
        #if k.name == 'c' and k.event_type == "down": key = 1
        #if k.name == 'space' and k.event_type == "down":
        time.sleep(0.01)

        if ai_x[0] == ai_x[1]: key = 0
        if ai_x[0] < ai_x[1]: key = -1
        if ai_x[0] > ai_x[1]: key = 1

        cpu.push_input(key)

    print("Game's over.")
示例#4
0
文件: 21.py 项目: pshatov/AoC
def main():

    opcodes = Intcode.load_opcodes('input.txt')

    _vt100_cursor_off()
    _vt100_cursor_xy(0, 0)

    cpu.reset(opcodes)

    #   A B C D
    # | . . . . | ?
    # | . . . X | JUMP
    # | . . X . |
    # | . . X X | JUMP
    # | . X . . |
    # | . X . X | JUMP
    # | . X X . |
    # | . X X X | JUMP
    # | X . . . |
    # | X . . X | JUMP
    # | X . X . |
    # | X . X X | JUMP
    # | X X . . |
    # | X X . X | JUMP
    # | X X X . |
    # | X X X X |

    # | . . . X | JUMP
    # | . . X X | JUMP
    # | . X . X | JUMP
    # | . X X X | JUMP
    # | X . . X | JUMP
    # | X . X X | JUMP
    # | X X . X | JUMP

    #
    # We must jump when D has ground and at least one tile in A..C has a hole:
    # J = D AND (NOT ((A AND B) AND C))
    #

    instr = [
        "NOT A J",  # J = NOT A
        "NOT J T",  # T = NOT J = NOT (NOT A) = A
        "AND B T",  # T = B AND T = A AND B
        "AND C T",  # T = C AND T = (A AND B) AND C
        "NOT T J",  # J = NOT T = NOT ((A AND B) AND C)
        "AND D J",  # J = D AND J = D AND (NOT ((A AND B) AND C))
        "WALK"
    ]

    for i in instr:
        if len(i) > 0:
            send_instr(i)

    cpu.run()

    while cpu.can_pop_output():
        p = cpu.pop_output()
        if p < 128:
            print(chr(p), end='')
        else:
            print("Result: %d" % p)
            break
示例#5
0
文件: 23.py 项目: pshatov/AoC
def main():

    opcodes = Intcode.load_opcodes('input.txt')

    for n in range(N):
        NICS.append(Intcode())
        NICS[n].reset(opcodes)
        NICS[n].push_input(n)
        NICS[n].run()
        QUEUE_FROM[n] = []
        QUEUE_TO[n] = []

    cycle = 0
    seen_first_nat = False
    seen_last_nat_y = -1
    while True:

        print("cycle = %d" % cycle)

        for n in range(N):
            nic = NICS[n]
            if nic.is_stopped():
                raise RuntimeError

        for n in range(N):
            nic = NICS[n]
            while nic.can_pop_output():
                byte = nic.pop_output()
                QUEUE_FROM[n].append(byte)
                print("#%02d >> %d" % (n, byte))

        for n in range(N):
            qf = QUEUE_FROM[n]
            while len(qf) >= 3:
                z = qf.pop(0)
                x = qf.pop(0)
                y = qf.pop(0)
                if z == BCAST:
                    NAT.clear()
                    NAT.append(x)
                    NAT.append(y)
                    print("%d, %d >> NAT" % (x, y))
                    if not seen_first_nat:
                        print("seen_first_nat == %s" % seen_first_nat)
                        print("Press space to continue...")
                        sys.stdout.flush()
                        wait_key()
                        seen_first_nat = True
                else:
                    qt = QUEUE_TO[z]
                    qt.append(x)
                    qt.append(y)
                    print("%d, %d >> #%02d" % (x, y, z))

        net_is_idle = True
        for n in range(N):
            nic = NICS[n]
            if nic.is_waiting_input():
                q = QUEUE_TO[n]
                if len(q) > 0: net_is_idle = False
                if len(q) < 2:
                    nic.push_input(-1)
                    nic.run()
                else:
                    x = q.pop(0)
                    y = q.pop(0)
                    nic.push_input(x)
                    nic.push_input(y)
                    nic.run()
            else:
                raise RuntimeError

        if cycle > 0 and net_is_idle:
            print("network is idle...")
            qt = QUEUE_TO[GW]
            x = NAT[0]
            y = NAT[1]
            qt.append(x)
            qt.append(y)
            print("%d, %d >> GW" % (x, y))

            if y == seen_last_nat_y:
                print("seen_last_nat_y == %d" % seen_last_nat_y)
                print("Press space to finish...")
                sys.stdout.flush()
                wait_key()
                return

            seen_last_nat_y = y

        cycle += 1