示例#1
0
def cameraOutput(defaultColor=0):
    code = getInputData()
    IC = IntcodeComputer(code)

    step = 0
    inp = []
    IC._output = []  # Clear the output list
    terminate = False
    stoppedAtInput = False
    while not terminate:
        while not stoppedAtInput and not terminate:
            terminate, stoppedAtInput = IC.perform_one_operation(
                IC._memoryPosition, inp, stopAtInput=True)
        step += 1
        # print(f'### STEP {step} ###')
        # print(f'OutPut{IC._output}')
        # print(f'OutPut{IC._output}')
        stoppedAtInput = False

    ret = decodeMap(IC._output)
    # ret = []
    # row = ''
    # for ascii in IC._output:
    #     if str(ascii) == '10':
    #         ret.append(row)
    #         row = ''
    #     else:
    #         row += chr(ascii)
    return ret
示例#2
0
def runDroid(instructions):
    trystring = getTryStr()
    droidController = getInputData()
    IC = IntcodeComputer(droidController)
    saved_Controller = copy.deepcopy(IC._intCodeProgramDict)
    saved_memPos = copy.deepcopy(IC._memoryPosition)
    saved_relativeBase = copy.deepcopy(IC._relativeBase)
    inp = []
    stoppedAtInput = False
    terminated = False
    autotry = False

    while not terminated:
        # print(inp)
        # print(terminated, stoppedAtInput)
        while not terminated and not stoppedAtInput:
            terminated, stoppedAtInput = IC.perform_one_operation(input=inp,stopAtInput=True)
            #print('hej')
    
        if stoppedAtInput:
            outCode = IC._output
            print(ascii2str(outCode))
            if not autotry: inpStr = input('> ')
            if inpStr == 'save':
                saved_Controller = copy.deepcopy(IC._intCodeProgramDict)
                saved_memPos = copy.deepcopy(IC._memoryPosition)
                print('\nGame saved\n\n')
                f = open("25save", "w")
                f.write("Now the file has more content!")
                f.close()
                inpStr = input('> ')
            if inpStr == 'load':
                IC._intCodeProgramDict = copy.deepcopy(saved_Controller)
                IC._memoryPosition = copy.deepcopy(saved_memPos) 
                IC._relativeBase = copy.deepcopy(saved_relativeBase)
                print('\nGame loaded\n\n')
                #outCode = IC._output
                #print(ascii2str(outCode))
                inpStr = input('> ')
            if inpStr == 'FF':
                inpStr = 'south\nwest\nnorth\ntake fuel cell\nsouth\neast\nnorth\nnorth\neast\ntake candy cane\nsouth\ntake hypercube\nnorth\nwest\nnorth\ntake coin\neast\ntake tambourine\nwest\nwest\ntake spool of cat6\nnorth\ntake weather machine\nwest\ntake mutex\nwest\ndrop spool of cat6\ndrop hypercube\ndrop weather machine\ndrop coin\ndrop candy cane\ndrop tambourine\ndrop fuel cell\ndrop mutex\ninv'
            if inpStr == 'autotry':
                autotry = True
                inpStr = '\nwest'
            if autotry:
                inpStr = trystring.pop(0) # pop first item
                print(inpStr)
            print('\n')
            inp = str2asciiList(inpStr + '\n')
            stoppedAtInput = False
    outCode = IC._output
    outstr = ascii2str(outCode)
    if outstr.find('Alert'):
        print('****** ALERT *********')
        
    print(outstr)
    return outstr
示例#3
0
def test_igen():
    prg = [
        3, 26, 1001, 26, -4, 26, 3, 27, 1002, 27, 2, 27, 1, 27, 26, 27, 4, 27,
        1001, 28, -1, 28, 1005, 28, 6, 99, 0, 0, 5
    ]
    IC = IntcodeComputer(prg)
    inp = [5, 0, 100]
    stoppedAtInput = False
    terminate = False
    print(IC._intCodeProgramDict)
    print(IC._output)
    while not stoppedAtInput and not terminate:
        terminate, stoppedAtInput = IC.perform_one_operation(input=inp)
        print(IC._intCodeProgramDict)
        print(IC._output)
示例#4
0
def startRobot(defaultColor=0):
    code = getInputData()
    IC = IntcodeComputer(code)

    robotOnMap = SolarPanelMap(defaultColor=defaultColor)

    step = 0
    inp = [defaultColor]
    IC._output = []  # Clear the output list
    terminate = False
    stoppedAtInput = False
    while not terminate:
        while not stoppedAtInput and not terminate:
            terminate, stoppedAtInput = IC.perform_one_operation(
                IC._memoryPosition, inp, stopAtInput=True)
        step += 1
        # print(f'### STEP {step} ###')
        # print(f'OutPut{IC._output}')
        paintBlack = (IC._output.pop(0) == 0)
        # print(f'OutPut{IC._output}')

        turnLeft = (IC._output.pop(0) == 0)
        # print(f'OutPut{IC._output}')

        if paintBlack:
            robotOnMap.paintBlack()
        else:
            robotOnMap.paintWhite()

        if turnLeft:
            robotOnMap.turnLeft()
        else:
            robotOnMap.turnRight()

        robotOnMap.moveForward()

        color = robotOnMap.getColor()  # 0=black, 1=white
        inp = [color]
        stoppedAtInput = False
    val = robotOnMap.countPaintedPanels()
    return val, robotOnMap._panelMap
示例#5
0
def day07PartTwo_old():
    '''
    #########################
    #  NOT YEY IMPLEMENTED  #
    #########################
    '''
    thrusterDict = {}
    amplifierController = getInputData()
    # l = [itertools.permutations([5,6,7,8,9], 5)]
    # print(l)
    # input()
    for seq in itertools.permutations([5, 6, 7, 8, 9], 5):
        print(f'----- {seq} -----')
        ICA = IntcodeComputer(amplifierController)
        ICB = IntcodeComputer(amplifierController)
        ICC = IntcodeComputer(amplifierController)
        ICD = IntcodeComputer(amplifierController)
        ICE = IntcodeComputer(amplifierController)

        ICA.perform_one_operation(input=[seq[0]])
        ICB.perform_one_operation(input=[seq[1]])
        ICC.perform_one_operation(input=[seq[2]])
        ICD.perform_one_operation(input=[seq[3]])
        ICE.perform_one_operation(input=[seq[4]])

        terminateE = False
        inA = 0
        step = 0
        while not terminateE:
            step += 1
            print(step)
            stoppedAtInputA = False
            terminateA = False
            stoppedAtInputB = False
            terminateB = False
            stoppedAtInputC = False
            terminateC = False
            stoppedAtInputD = False
            terminateD = False
            stoppedAtInputE = False
            terminateE = False

            while not stoppedAtInputA and not terminateA:
                terminateA, stoppedAtInputA = ICA.perform_one_operation(
                    input=[inA])
            outA = ICA._output.pop()
            print(f'A:{outA}, {terminateA}, {stoppedAtInputA}')

            while not stoppedAtInputB and not terminateB:
                terminateB, stoppedAtInputB = ICB.perform_one_operation(
                    input=[outA])
            outB = ICB._output.pop()
            print(f'B:{outB}, {terminateB}, {stoppedAtInputB}')

            while not stoppedAtInputC and not terminateC:
                terminateC, stoppedAtInputC = ICC.perform_one_operation(
                    input=[outB])
            outC = ICC._output.pop()
            print(f'C:{outC} , {terminateC}, {stoppedAtInputC}')

            while not stoppedAtInputD and not terminateD:
                terminateD, stoppedAtInputD = ICD.perform_one_operation(
                    input=[outC])
            outD = ICD._output.pop()
            print(f'D:{outD}, {terminateD}, {stoppedAtInputD}')

            while not stoppedAtInputE and not terminateE:
                terminateE, stoppedAtInputE = ICE.perform_one_operation(
                    input=[outD])
            outE = ICE._output.pop()
            print(f'E:{outE}, {terminateE}, {stoppedAtInputE}')
            inA = outE

        thrusterDict[seq] = outE

    maxSetting = max(thrusterDict, key=thrusterDict.get)
    maxSignal = max(thrusterDict.values())
    print(thrusterDict)
    print(maxSetting)
    print(maxSignal)
    return maxSignal

    answer = "unknown"
    print(f'Solution Day XX, Part two:\nAnswer: {answer} \n\n')
示例#6
0
def runGame(display):

    code = getInputData()
    IC = IntcodeComputer(code)
    # game = PILScreen()
    if display:
        game = Blocker(841, 600)
    count = 0

    paddlePos = 0

    # Set memory position 0 to 2, to play for free
    # IC.writeMem(0,2)

    ##################
    step = 0
    inp = []  #input??
    IC._output = []  # Clear the output list
    terminate = False
    stoppedAtInput = False
    HiScore = 0
    while not terminate:
        length = 0
        while length < 3 and not terminate:
            terminate, stoppedAtInput = IC.perform_one_operation(
                input=inp, stopAtInput=True)
            length = len(IC._output)

        # print(f'OutPut: {IC._output}')
        # print(f'Terminate: {terminate}')
        # print(f'stoppedAtInput: {stoppedAtInput}')
        # print(f'count: {count}')
        # print(f'Next instr: {IC._intCodeProgramDict[IC._memoryPosition]}')
        # print(f'Next memPos:{IC._memoryPosition}')

        if terminate: break

        try:
            x = IC._output[0]
            y = IC._output[1]
            t = IC._output[2]
            IC._output = []
        except Exception:
            print('EXCEPTION')
            break
            #game.show()

        ########   TILES
        #
        #   0 is an empty tile. No game object appears in this tile.
        #   1 is a wall tile. Walls are indestructible barriers.
        #   2 is a block tile. Blocks can be broken by the ball.
        #   3 is a horizontal paddle tile. The paddle is indestructible.
        #   4 is a ball tile. The ball moves diagonally and bounces off objects.

        if t == 3:
            paddlePos = x

        if t == 4:
            if paddlePos < x: inp = [1]
            elif paddlePos > x: inp = [-1]
            else: inp = [0]

        if x == -1 and y == 0:  # -1, 0, t gives SCORE
            if t > HiScore: HiScore = t
            if display: game.draw_text(str(HiScore))
        else:
            if t == 2: count += 1
            #if t != 0:
            if display: game.drawBrick(x, y, t)
        if display: game.screen_update()

    return count, HiScore
示例#7
0
 def test_less2(self):
     IC = IntcodeComputer([1107, 2, 1, 3, 99]) # greater than
     IC.perform_one_operation(0)
     self.assertEqual(list(IC._intCodeProgramDict.values()),[1107, 2, 1, 0, 99])
     self.assertEqual(IC._memoryPosition,4)
示例#8
0
 def test_equal2(self):
     IC = IntcodeComputer([1108, 2, 1, 3, 99]) # not equal
     IC.perform_one_operation(0)
     self.assertEqual(IC.readMem(3), 0)
     self.assertEqual(IC._memoryPosition,4)
示例#9
0
 def test_jumpFalse3(self):
     IC = IntcodeComputer([1006, 4, 5, 99, 0, 6 ]) # do jump to pos 5
     IC.perform_one_operation(0)
     self.assertEqual(list(IC._intCodeProgramDict.values()),[1006, 4, 5, 99, 0, 6])
     self.assertEqual(IC._memoryPosition,5)
示例#10
0
 def test_jumpFalse(self):
     IC = IntcodeComputer([1106, 0, 4, 4, 99])
     IC.perform_one_operation(0)
     self.assertEqual(list(IC._intCodeProgramDict.values()),[1106, 0, 4, 4, 99])
     self.assertEqual(IC._memoryPosition,4)
示例#11
0
 def test_add(self):
     IC = IntcodeComputer([1001,2,2,4,-1])
     IC.perform_one_operation(0)
     self.assertEqual(list(IC._intCodeProgramDict.values()),[1001,2,2,4,4])
示例#12
0
 def test_multiply(self):
     IC = IntcodeComputer([1002,4,3,4,1])
     IC.perform_one_operation(0)
     self.assertEqual(list(IC._intCodeProgramDict.values()),[1002,4,3,4,3])
示例#13
0
class RepairDroid():
    def __init__(self, startPostitonX = 0, startPostitonY = 0):
        #self._currentPositionX = startPostitonX   # Cartesian coordinates
        #self._currentPositionY = startPostitonY
        self._currentposition = (startPostitonX, startPostitonY)
        # self._currentDirection = 'U' # The robot starts facing up. (U, D, L, R)
        self._visitedMap = {(startPostitonX,startPostitonY):True} # {position(x,y):visited True/False}
        self._goalpos = (None, None)
        self._goalFound = False
        self._stepsToGoal = 0
        prg = getInputData()
        self.IC = IntcodeComputer(prg)
        self.dir = {1:(0,-1), 2:(0,1), 3:(-1,0), 4:(1,0)} 

    # north (1) 
    # south (2)
    # west (3)
    # east (4)
    
    def goNorth(self):
        moved, foundGoal = self._go(1)
        if moved: 
            self._currentPositionY -=1
            self._visitedMap[(self._currentPositionX,self._currentPositionY)] = True
        else:
            self._visitedMap[(self._currentPositionX,self._currentPositionY-1)] = False 
        if foundGoal:
            self._goalPosX = self._currentPositionX
            self._goalPosY = self._currentPositionX
            self._goalPosY = True

        return self._currentPositionX, self._currentPositionY, moved

    def goSouth(self):
        moved, foundGoal = self._go(2)
        
        if moved:
            self._currentPositionY +=1
            self._visitedMap[(self._currentPositionX,self._currentPositionY)] = True
        else:
            self._visitedMap[(self._currentPositionX,self._currentPositionY+1)] = False

        if foundGoal:
            self._goalPosX = self._currentPositionX
            self._goalPosY = self._currentPositionX
            self._goalPosY = True

        
        return self._currentPositionX, self._currentPositionY, moved

    def goWest(self):
        
        moved, foundGoal = self._go(3)
        if moved:
            self._currentPositionX -=1
            self._visitedMap[(self._currentPositionX,self._currentPositionY)] = True
        else:
            self._visitedMap[(self._currentPositionX-1,self._currentPositionY)] = False
        if foundGoal:
            self._goalPosX = self._currentPositionX
            self._goalPosY = self._currentPositionX
            self._goalPosY = True


        return self._currentPositionX, self._currentPositionY, moved

    def goEast(self):
        
        moved, foundGoal = self._go(4)
        if moved:
            self._currentPositionX +=1
            self._visitedMap[(self._currentPositionX,self._currentPositionY)] = True
        else:
            self._visitedMap[(self._currentPositionX+1,self._currentPositionY)] = False

        if foundGoal:
            self._goalPosX = self._currentPositionX
            self._goalPosY = self._currentPositionX
            self._goalPosY = True

        return self._currentPositionX, self._currentPositionY, moved

    def processInstruction(self, instr):      
        stoppedAtInput = False
        terminated = False
        while not terminated:
            print(f'entering whileLoop:{terminated}, {stoppedAtInput}\n')
            while not terminated and not stoppedAtInput:
                terminated, stoppedAtInput = self.IC.perform_one_operation(input=instr,stopAtInput=True)
                
        
            if stoppedAtInput:
                print('Time to stop, input.')
                break
       # print(f'Outside loop:{terminated}, {stoppedAtInput}')        
       # print(f'OUTPUT:self.IC._output')
        o=""
        if len(self.IC._output) > 0:
       #    print('popping')
            o = self.IC._output.pop()
        return o

    def _go(self, dir):
        goalFound = False
        o = self.processInstruction([dir])
        # 0: The repair droid hit a wall. Its position has not changed.
        # 1: The repair droid has moved one step in the requested direction.
        # 2: The repair droid has moved one step in the requested direction; its new position is the location of the oxygen system.
        print(f'OUTPUT:{o}')
        if o == 0:
            moved = False
        elif o==1:
            moved = True
        elif o==2:
            moved = True
            self._goalFound = True
            goalFound = True
        else:

            assert False, 'You should not be here'
        return moved, goalFound



    def goExplore(self, fromXY, direction, steps,labytinthDisplay):
        labytinthDisplay.drawLabytinth(self._visitedMap)
        labytinthDisplay.drawDroid(self._currentposition[0],self._currentposition[1])
        if self._goalFound: 
            labytinthDisplay.drawGoal(self._goalpos[0], self._goalpos[1])
            text = self._stepsToGoal
        else:
            text = steps
        labytinthDisplay.drawStart(21,21)
        labytinthDisplay.draw_text(str(text))
        labytinthDisplay.screen_update()
    
        #time.sleep(0.1)
        print(f'xy:{fromXY}')
        
            # north (1) 
            # south (2)
            # west (3)
            # east (4)
            
        def forward(dir):
            return dir

        def left(dir):
            ndir = [-1, 3, 4, 2, 1]
            return ndir[dir]

        def right(dir):
            ndir = [-1, 4, 3, 1, 2]
            return ndir[dir]
        
        def back(dir):
            ndir = [-1, 2, 1, 4, 3]
            return ndir[dir]

        goalFound = False
        print(f'Direction: {direction} = {self.dir[direction]}')
        newXY = (fromXY[0] + self.dir[direction][0],fromXY[1] + self.dir[direction][1]) 
        print(f'ExplorePos: {newXY}')
        print(f'steps: {steps}')
        
        o = self.processInstruction([direction])
        # 0: The repair droid hit a wall. Its position has not changed.
        # 1: The repair droid has moved one step in the requested direction.
        # 2: The repair droid has moved one step in the requested direction; its new position is the location of the oxygen system.
        print(f'OUTPUT:{o}')
        if o == 0:
            moved = False
             # self._currentPositionX and self._currentPositionY not changed
            self._visitedMap[newXY] = False # not path
        elif o==1:
            moved = True
            steps += 1
            self._currentposition = newXY
            self._visitedMap[newXY] = True # path!
        elif o==2:
            moved = True
            steps += 1
            self._goalFound = True
            self._currentposition = newXY
            self._visitedMap[newXY] = True # path! and goal
            self._goalpos = newXY
            self._stepsToGoal = steps
            goalFound = True
        else:
            assert False, 'You should not be here'
        
        if moved:
            # left then forward the right... follow thw left wall 
            goalFound = self.goExplore(copy.deepcopy(newXY), left(direction), steps, labytinthDisplay)         # explore to the left
            goalFound = self.goExplore(copy.deepcopy(newXY), forward(direction), steps, labytinthDisplay)      # explore same direction           
            goalFound = self.goExplore(copy.deepcopy(newXY), right(direction), steps, labytinthDisplay)        # explore to the right
            
            # wrong way, go back to where you came from
            o = self.processInstruction([back(direction)])
            self._currentposition = fromXY
            steps -=1 

        
        return goalFound