Example #1
0
def check_in_beam(pdata, xpos, ypos):

    p = Program(copy.copy(pdata))
    p.eval()

    assert (p.waitingForInput())
    p.pushInput(xpos)
    p.pushInput(ypos)
    output = p.eval()
    assert (len(output) == 1)
    return output[0] == 1
Example #2
0
def main():

    globalMap = Map()
    data = str_to_program(open(sys.argv[1]).read().rstrip('\n'))

    explorationNum = 1

    while not globalMap.isComplete():
        print(
            f"Beginning exploration {explorationNum} with {globalMap.size()}")

        p = Program(data)
        e = Explore()
        found = False
        i = 0
        lastExplored = 0
        while not p.isHalted() and e.explorationSpace() < 1600:
            p.pushInput(e.nextCommand())
            output = p.eval()
            e.status(output)
            found = output[0] == 2
            if i % 10000 == 0:
                print(
                    f"Exploration {explorationNum} Iteration {i} has explored {e.explorationSpace()}"
                )
                if e.explorationSpace() == lastExplored:
                    print(f"Restarting map exploration")
                    break
                lastExplored = e.explorationSpace()
            i += 1
        explorationNum += 1
        globalMap.update(e.map)
        print(f"Executing autofill")
        globalMap.autofill()
        globalMap.display()

    #print(f"yrange: {miny,maxy} and {minx,maxx}")
    moves = astar_search(globalMap, (0, 0), globalMap.target)
    print(f"Moves to target from origin: {moves}")

    #e.display()

    print(f"{fill_space(globalMap, globalMap.target)} is the answer")
Example #3
0
def main():
    i = 0

    parser = argparse.ArgumentParser()
    parser.add_argument('infile', help='input file', type=str)
    parser.add_argument('--run',
                        help='run instead of walk',
                        action='store_true')
    args = parser.parse_args()

    pdata = str_to_program(open(args.infile).read().rstrip('\n'))
    solved = False
    plen = 1
    while not solved:
        if i % 100 == 0:
            print("Program {}".format(i))
        p = Program(pdata)
        output = p.eval()
        if p.waitingForInput():
            instructions = RUN_PROGRAM if args.run else WALK_PROGRAM

            for ins in instructions:
                [p.pushInput(ord(c)) for c in ins]

            output = p.eval()
            if output[-1] > 255:
                print(f"Solved: {int(output[-1])}")
            else:
                for c in output:
                    print(chr(c), end='')
            #solved = output[0] != ord('.') and output[0] != ord('#') and output[0] != ord('@') and output[0] != ord('\n')
            #if solved:
            #print("Program {} solved with {}".format(i, instructions))
            #print(output)
            solved = True
        i += 1
Example #4
0
BLACK = 0
WHITE = 1

POS_DELTA = {UP: (1, 0), RIGHT: (0, 1), DOWN: (-1, 0), LEFT: (0, -1)}

if __name__ == "__main__":
    pdata = []
    for line in open(sys.argv[1]).readlines():
        pdata.extend(str_to_program(line))

    p = Program(pdata)
    pos = (0, 0)
    positions = [pos]
    position_color_map = defaultdict(int)
    position_color_map[pos] = WHITE
    p.pushInput(position_color_map[pos])
    dir = UP
    while not p.isHalted():
        if p.waitingForInput():
            #print(f"INPUT: {position_color_map[pos]}")
            # v = input()
            p.pushInput(position_color_map[pos])
        color, turn = p.eval()
        dir = (dir + 4 + (1 if turn == 1 else -1)) % 4
        #print(f"Turn {turn}")
        #print(f"New Direction {dir}")
        position_color_map[pos] = color
        new_pos = (pos[0] + POS_DELTA[dir][0], pos[1] + POS_DELTA[dir][1])
        pos = new_pos
        #print(f"New Position {pos}")
Example #5
0
def main():
    inp = str_to_program(open(sys.argv[1]).read().rstrip('\n'))
    inp[0] = 2
    p = Program(inp)
    output = []
    joystick_input = None 
    score = 0
    tiles = {}
    while not p.isHalted():
        print("==================================================================================")
        if joystick_input != None:
            p.pushInput(joystick_input)
        output = p.eval()
        blockTileCount = 0
        for i in range(0,len(output),3):
            if output[i] == -1 and output[i+1] == 0:
                score = output[i+2]
            else:
                x,y = output[i],output[i+1]

                if output[i+2] == 2:
                    blockTileCount += 1

                tiles[(x,y)] = output[i+2]
        keys = tiles.keys()
        keys = sorted(keys)
        minX = min([x[0] for x in keys])
        maxX = max([x[0] for x in keys])
        minY = min([x[1] for x in keys])
        maxY = max([x[1] for x in keys])

        paddle_x = -1
        ball_x = -1
        for j in range(minY, maxY, 1):
            for i in range(minX, maxX, 1):
                #print(f"Reading tile {(i,j)}")
                t = tiles[(i,j)]
                if t == 0:
                    print(".",end="")
                elif t == 1:
                    print("|",end="")
                elif t == 2:
                    print("#", end="")
                elif t == 3:
                    print("_", end="")
                    paddle_x = i
                elif t == 4:
                    print("o", end="")
                    ball_x = i
                else:
                    assert(False)
            print()
        assert(paddle_x >= 0)
        assert(ball_x >= 0)
        if paddle_x < ball_x:
            joystick_input = 1
        elif paddle_x > ball_x:
            joystick_input = -1
        else:
            joystick_input = 0
        print(f"Ball at {ball_x} and paddle at {paddle_x} joystick {joystick_input}")
    print(f"Game ended with score {score}")