Esempio n. 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
Esempio n. 2
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
Esempio n. 3
0
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}")

        if pos not in positions:
            positions.append(pos)
        #print(f"Num Positions {len(positions)}")