예제 #1
0
def part2(data):
    print(data)
    data[0] = 2
    inputs = []
    intcoder = Intcode(data, inputs)
    score = 0
    # possible inputs
    outputs = []
    ballPos = 0
    padPos = 0
    while intcoder.running:
        # check for outputs
        if len(intcoder.output) == 3:
            # print("output",intcoder.output)
            o1, o2, o3 = intcoder.output
            if o3 == 4:
                ballPos = o1
                print("ballPos",ballPos,o2)
                # provide next input
                nextinput = 0
                sign = padPos - ballPos
                if sign > 0:
                    nextinput = -1
                elif sign < 0:
                    nextinput = 1
                inputs.append(nextinput)
                print("nextinput",nextinput)
            if o3 == 3:
                padPos = o1
                print("padPos",padPos,o2)
                # provide next input
                nextinput = 0
                sign = padPos - ballPos
                if sign > 0:
                    nextinput = -1
                elif sign < 0:
                    nextinput = 1
                inputs.append(nextinput)
                print("nextinput",nextinput)
            if o1 == -1 and o2 == 0:
                score = o3
            outputs.append(intcoder.output)
            intcoder.output = []
        intcoder.step()
    print("score",score)
예제 #2
0
def controlRobot(data, startPanelColor=0):
    panels = {(0, 0): startPanelColor}
    loc = (0, 0)
    robotdir = "up"
    robotsteps = 0

    intcode_input = [startPanelColor]
    intcode = Intcode(data, intcode_input, debug=False)

    while intcode.running:
        # check for outputs
        if len(intcode.output) >= 2:
            print("robot outputs:", intcode.output)
            # get output
            paintColor = intcode.output[0]
            newdir = intcode.output[1]

            # paint the current spot
            panels[loc] = paintColor
            print("painted ", loc, " ", paintColor)
            print("panels", panels)

            # move the robot
            print("robot was at:", loc, robotdir)
            loc1, loc2, robotdir = moveRobot(loc, robotdir, newdir)
            loc = (loc1, loc2)
            robotsteps += 1
            if robotsteps > 20000:
                print("ROBOT WENT TOO FAR")
                exit(0)

            # set the color for the new spot
            if panels.get(loc) is not None:
                intcode_input.append(panels[loc])
            else:
                intcode_input.append(0)  # black by default
            print("robot now at:", loc, robotdir)
            intcode.output = []
        intcode.step()
    return panels
예제 #3
0
def part1(data):
    print(data)
    random.seed(1)
    steps = 0

    # I randomly explored rooms to draw a map of the rooms
    # then was able to manually write these instructions to
    # visit all rooms with items we might want to take
    to_pressure_plate = ["east","east","west","west", # hull breach
                         "north","east","north","south","west", # gift wrap
                         "north","north","south","south","south", # hull breach
                         "west","west", # hallway
                         "south","east","north","south","west","north", # hallway
                         "north","east","east", # pressure room
                         "inv"]
    ppi = 0
    # manually figured out which items got us the correct weight
    do_not_take = [
        # things that cause you to die if you pick it up
        "photons", "infinite loop", "giant electromagnet", "escape pod", "molten lava",
        # figuring out what weight we need
        "ornament", # is too heavy by itself
        "semiconductor",
        "asterisk",
        "dark matter",
        # "sand",
        # "wreath",
        # "loom",
        # "mutex",
    ]

    intcode_input = [] #asciiToIntcodeInput(movement[0])
    intcoder = Intcode(data, intcode_input)
    while intcoder.running:
        # check output
        if len(intcoder.output) > 0:
            # check output as string
            s = ""
            for o in intcoder.output:
                s += str(chr(o))
            if s[-8:] == "Command?":
                # provide command somehow
                print(s)

                # if we previously took something, other inputs haven't changed,
                if "You take" in s:
                    items = []
                else:
                    # parse output string
                    doors = []
                    items = []

                    doorsi = s.find("Doors here lead:")
                    itemsi = s.find("Items here:")


                    # parse movements
                    if doorsi != -1:
                        doorss = s[doorsi+16:itemsi]
                        ss = doorss.split("- ")
                        doors = ss[1:]
                        doors = [d.split("\n")[0] for d in doors]
                        print("Doors",doors)

                    # parse items
                    if itemsi != -1:
                        itemss = s[itemsi + 11:]
                        ss = itemss.split("- ")
                        items = ss[1:]
                        items = [d.split("\n")[0] for d in items]
                        print("Items",items)


                # somehow decide what to do
                nextInput = ""

                if len(items) > 0 and items[0] not in do_not_take:
                    nextInput = "take " + items[0]
                # go next direction on route
                elif len(doors) > 0:
                    # choose randomly to do initial exploration
                    # random.shuffle(doors)
                    # nextInput = doors[0]
                    nextInput = to_pressure_plate[ppi]
                    ppi += 1
                print("Providing input:",nextInput)
                intcode_input += asciiToIntcodeInput(nextInput)
                intcoder.output = []
        intcoder.step()
        steps += 1

    s = ""
    for o in intcoder.output:
        s += str(chr(o))
    print(s)