Beispiel #1
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("infile", type=str, help="program source")
    args = parser.parse_args()

    pdata = str_to_program(open(sys.argv[1]).read().rstrip('\n'))
    count = 0

    consecY = [0] * 500

    sleigh_size = 1

    debug = False
    ypos = 30
    xleft = 0
    lastdx = 100
    done = False
    while not done:
        xleft, lastdx, xright = find_beam_edgex(pdata, ypos, xleft, lastdx)
        if xleft == -1:
            print(f"Unable to find beam for ypos {ypos}")
            exit(-1)
        print(f"Beam for y {ypos} from {xleft} to {xright}")
        ypos += 1
        if xright - xleft >= 99:
            xpos = xleft
            while (xpos + 99) <= xright:
                if check_square(pdata, xpos, ypos):
                    print(f"Found beam with square 100 at {xpos, ypos}")
                    done = True
                xpos += 1
Beispiel #2
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("infile", type=str, help="program source")
    parser.add_argument("--buildmap",
                        help="build out the map",
                        action="store_true",
                        default=False)
    parser.add_argument
    args = parser.parse_args()

    p = Program(str_to_program(open(sys.argv[1]).read().rstrip('\n')))

    if args.buildmap:
        buildmap(p)
    else:
        driverobot(p)
Beispiel #3
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")
Beispiel #4
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
Beispiel #5
0
import sys
from collections import deque
from utils import str_to_program
from intcode import Program
if __name__ == "__main__":
    inputQ = deque()

    for line in open(sys.argv[1]).readlines():
        p = Program(str_to_program(line))
        out = p.eval()
        print(f"out: {out}")
        #v = eval_program(str_to_program(line), 0, inputQ, 0)
Beispiel #6
0
import numpy as np

UP = 0
RIGHT = 1
DOWN = 2
LEFT = 3

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
Beispiel #7
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}")
Beispiel #8
0
def main():
    for line in open(sys.argv[1]).readlines():
        program = str_to_program(line)
        optimize_program(program)
Beispiel #9
0
    elif opcode == 9:
        relative_base += inp[0]
        extend_prog_data(prog_data, len(prog_data) + relative_base*2)
        print(f"New relative base {relative_base}")
    elif opcode == 4:
        print(f"Appending to outputQ {prog_idx}")
        outputQ.append(inp[0])
    elif opcode == 99:
        assert(num_inp == 0)
        assert(num_outp == 0)
        print("HALT")
        return len(prog_data),-1
    return ((prog_idx + 1 + num_inp + num_outp), relative_base) if new_prog_idx == -1 else (new_prog_idx, relative_base)

def eval_program(prog_data, prog_idx, inputQ, relative_base):
    outputQ = deque()
    extend_prog_data(prog_data, len(prog_data)*5)
    while prog_idx < len(prog_data):
        prog_idx, relative_base = eval_instruction(prog_data, prog_idx, inputQ, outputQ, relative_base)
        #print(f"{prog_idx} / {len(outputQ)}")
        assert(prog_idx >= 0)
    print(",".join([f"{x}" for x in outputQ]))
    return prog_idx, outputQ.popleft() if len(outputQ) > 0 else -1

if __name__ == "__main__":
    inputQ = deque()

    for line in open(sys.argv[1]).readlines():
        v = eval_program(str_to_program(line), 0, inputQ, 0)
        #print(f"OUTPUT {v}")