Beispiel #1
0
def Part1(lines):
    grid = com.CartesianGrid()
    currentX = 0
    currentY = 0

    def outputCallback(output):
        nonlocal grid, currentX, currentY
        value = chr(output)
        if output == 10:
            currentY += 1
            currentX = 0
            print('\n', end='')
        else:
            currentPoint = com.Point(currentX, currentY, value)
            grid.addPoint(currentPoint)
            currentX += 1
            print(value, end='')

    intCode = com.intCode(lines[0], False, hasOutputCallback=outputCallback)
    intCode.RunIntCodeComputer()

    total = 0
    for point in grid.getAllPoints():
        if point.data == '#':
            up = grid.getPoint(point.x, point.y + 1)
            if up is not None and up.data == '#':
                down = grid.getPoint(point.x, point.y - 1)
                if down is not None and down.data == '#':
                    left = grid.getPoint(point.x - 1, point.y)
                    if left is not None and left.data == '#':
                        right = grid.getPoint(point.x + 1, point.y)
                        if right is not None and right.data == '#':
                            total += (point.x * point.y)
    print(total)
Beispiel #2
0
def Part1(lines):
    line = lines[0]
    amps = [com.intCode(line, printOutput=False) for i in range(5)]
    maxOutput = 0
    # probably a way better way to do this
    for a in range(5):
        for b in range(5):
            if b == a:
                continue
            for c in range(5):
                if c == a or c == b:
                    continue
                for d in range(5):
                    if d == a or d == b or d == c:
                        continue
                    for e in range(5):
                        if e == a or e == b or e == c or e == d:
                            continue
                        amps[0].setPresetInputs([a, 0])
                        amps[0].RunIntCodeComputer()
                        amps[1].setPresetInputs([b, amps[0].outputValue])
                        amps[1].RunIntCodeComputer()
                        amps[2].setPresetInputs([c, amps[1].outputValue])
                        amps[2].RunIntCodeComputer()
                        amps[3].setPresetInputs([d, amps[2].outputValue])
                        amps[3].RunIntCodeComputer()
                        amps[4].setPresetInputs([e, amps[3].outputValue])
                        amps[4].RunIntCodeComputer()
                        if maxOutput < amps[4].outputValue:
                            maxOutput = amps[4].outputValue
    print(maxOutput)
Beispiel #3
0
def Part1(lines):
    x = None
    y = None
    tid = None
    grid = com.CartesianGrid(' ')
    numBlocks = 0

    def outputCallback(output):
        nonlocal x, y, tid, grid, numBlocks
        if x is None:
            x = output
        elif y is None:
            y = output
        elif tid is None:
            tid = output
            point = com.Point(x, y, tid)
            if tid == 2:
                numBlocks += 1
            grid.addPoint(point)
            x = None
            y = None
            tid = None

    compy = com.intCode(lines[0],
                        printOutput=False,
                        hasOutputCallback=outputCallback)
    compy.RunIntCodeComputer()
    print(numBlocks)
Beispiel #4
0
def Part1(lines):
    noun = None
    verb = None
    if not test:
        noun = 12
        verb = 2
    computer = com.intCode(lines[0])
    print(str(computer.RunIntCodeComputer(noun, verb, True)))
Beispiel #5
0
def Part2(lines):
    grid = com.CartesianGrid()
    currentX = 0
    currentY = 0
    gridFinished = False

    def outputCallback(output):
        nonlocal grid, currentX, currentY, gridFinished
        if not gridFinished:
            if output > 256:
                print(output)
                return
            value = chr(output)
            if output == 10:
                currentY += 1
                currentX = 0
                print('\n', end='')
            else:
                currentPoint = com.Point(currentX, currentY, value)
                grid.addPoint(currentPoint)
                currentX += 1
                print(value, end='')
        else:
            print(output)

    inputNum = 0
    localInputNum = 0

    def inputCallback():
        nonlocal inputNum, gridFinished, localInputNum
        value = None
        #gridFinished = True
        if inputNum == 0:  # main
            value = toAsciiInts('A,C,A,C,B,B,C,B,C,A\n')
        elif inputNum == 1:  # A
            value = toAsciiInts('R,12,L,8,R,12\n')
        elif inputNum == 2:  # B
            value = toAsciiInts('R,8,L,8,R,8,R,4,R,4\n')
        elif inputNum == 3:  # C
            value = toAsciiInts('R,8,R,6,R,6,R,8\n')
        elif inputNum == 4:  # video
            value = toAsciiInts('n\n')

        character = value[localInputNum]
        if character == 10:
            inputNum += 1
            localInputNum = 0
        else:
            localInputNum += 1
        return character

    stringList = list(lines[0])
    stringList[0] = '2'
    intCode = com.intCode(''.join(stringList),
                          False,
                          needsInputCallback=inputCallback,
                          hasOutputCallback=outputCallback)
    intCode.RunIntCodeComputer()
Beispiel #6
0
def Part2(lines):
    neededResult = 19690720
    computer = com.intCode(lines[0])
    for noun in range(0, 100):
        for verb in range(0, 100):
            result = computer.RunIntCodeComputer(noun, verb, False)
            if result == neededResult:
                print(str(100 * noun + verb))
                return
    print("No answer")
Beispiel #7
0
def Part1(lines):
    global compys, grid, keepRunning
    compys.append([None, [0, 0], [], deque()])
    origin = com.Point(0, 0, None)
    grid.addPoint(origin)
    intCode = com.intCode(lines[0],
                          False,
                          hasOutputCallback=outputCallback(0),
                          needsInputCallback=inputCallback(0))
    compys[0][0] = intCode
    while keepRunning:
        intCode.RunIntCodeComputer()
Beispiel #8
0
def Part2(lines):
    x = None
    y = None
    tid = None
    grid = com.CartesianGrid(' ', cellOutputStrFcn=cellStr)
    score = 0

    ball = None
    paddle = None

    def inputCallback():
        nonlocal grid, ball, paddle
        #print(grid)
        if ball.x == paddle.x:
            return 0
        elif ball.x < paddle.x:
            return -1
        else:
            return 1

    def outputCallback(output):
        nonlocal x, y, tid, grid, score, ball, paddle
        if x is None:
            x = output
        elif y is None:
            y = output
        elif tid is None:
            tid = output
            point = com.Point(x, y, tid)
            if tid > 4 and x == -1 and y == 0:
                score = tid
            else:
                if tid == 4:
                    ball = point
                elif tid == 3:
                    paddle = point
                grid.addPoint(point)
            x = None
            y = None
            tid = None

    compy = com.intCode(lines[0],
                        printOutput=False,
                        hasOutputCallback=outputCallback,
                        needsInputCallback=inputCallback)
    compy.initialMemory[0] = 2
    compy.RunIntCodeComputer()
    print(score)
Beispiel #9
0
def runRobot(line, startingColor):
    grid = com.CartesianGrid(' ', cellStr)
    black = 0
    white = 1
    currentPoint = com.Point(0, 0, startingColor)
    grid.addPoint(currentPoint)
    painted = set()
    needsColorOutput = True
    up = [0, 1]
    right = [1, 0]
    down = [0, -1]
    left = [-1, 0]
    directions = [up, right, down, left]
    facingIdx = 0

    def outputCallback(output):
        nonlocal needsColorOutput, currentPoint, painted, directions, facingIdx
        if needsColorOutput:
            currentPoint.data = output
            painted.add(currentPoint)
            needsColorOutput = False
        else:
            if output == 0:
                facingIdx -= 1
            else:
                facingIdx += 1

            facingIdx %= 4
            nextX = currentPoint.x + directions[facingIdx][0]
            nextY = currentPoint.y + directions[facingIdx][1]
            currentPoint = grid.getPoint(nextX, nextY)
            if currentPoint is None:
                currentPoint = com.Point(nextX, nextY, 0)
                grid.addPoint(currentPoint)

            needsColorOutput = True

    def inputCallback():
        nonlocal currentPoint
        return currentPoint.data

    intcode = com.intCode(lines[0],
                          printOutput=False,
                          needsInputCallback=inputCallback,
                          hasOutputCallback=outputCallback)
    intcode.RunIntCodeComputer()
    return painted, grid
Beispiel #10
0
def Part2(lines):
    global amps, outputs, initialized, phases

    line = lines[0]
    amps = [
        com.intCode(line,
                    printOutput=False,
                    needsInputCallback=getNeedsInputCallback(i),
                    hasOutputCallback=getOutputCallback(i)) for i in range(5)
    ]
    maxOutput = 0
    # probably a way better way to do this
    for a in range(5, 10):
        for b in range(5, 10):
            if b == a:
                continue
            for c in range(5, 10):
                if c == a or c == b:
                    continue
                for d in range(5, 10):
                    if d == a or d == b or d == c:
                        continue
                    for e in range(5, 10):
                        if e == a or e == b or e == c or e == d:
                            continue
                        initialized = [False for i in range(5)]
                        phases = [a, b, c, d, e]
                        # Give the last amp a 0 output to start off the chain
                        outputs = [None, None, None, None, 0]
                        ampIdx = 0
                        for amp in amps:
                            amp.Initialize()
                        while True:
                            result = amps[ampIdx].Run()
                            if result != amps[ampIdx].PAUSED:
                                break
                            ampIdx = (ampIdx + 1) % len(amps)
                        if maxOutput < amps[4].outputValue:
                            maxOutput = amps[4].outputValue
    print(maxOutput)
Beispiel #11
0
def Part1(lines):
    noun = None
    verb = None
    computer = com.intCode(lines[0])
    computer.RunIntCodeComputer(noun, verb, False)
Beispiel #12
0
def Part1(lines):
    compy = com.intCode(lines[0])
    compy.RunIntCodeComputer(debug=False)
Beispiel #13
0
def Part1(lines):
    global stop, y
    y = 0
    intCode = com.intCode(lines[0], printOutput=False, needsInputCallback=inputFcn, hasOutputCallback=outputFcn)
    while not stop:
        intCode.RunIntCodeComputer()
Beispiel #14
0
def Part2(lines):
    OPEN = 1
    WALL = 0
    currentPoint = com.Point(0, 0, OPEN)
    nextPoint = None
    north = [0, 1]
    east = [1, 0]
    south = [0, -1]
    west = [-1, 0]
    directions = [east, north, west, south]
    directionInputs = [4, 1, 3, 2]
    facingIdx = 0
    turned = False
    maxSteps = 9999999999999
    def outputPoint(point):
        nonlocal facingIdx, currentPoint
        if point.data == 0:
            return '#'
        elif point == currentPoint:
            if facingIdx == 0:
                return '>'
            elif facingIdx == 1:
                return '^'
            elif facingIdx == 2:
                return '<'
            elif facingIdx == 3:
                return 'v'
        return '.'
    grid = com.CartesianGrid(' ', outputPoint)
    grid.addPoint(currentPoint)

    def inputHandler():
        nonlocal grid, currentPoint, OPEN, WALL, north, east, south, west, directions, facingIdx, directionInputs, nextPoint, turned
        while True:
            leftIdx = (facingIdx + 1) % 4
            newCoords = currentPoint + directions[leftIdx]
            leftPoint = grid.getPoint(newCoords[0], newCoords[1])
            if leftPoint is None:
                nextPoint = com.Point(newCoords[0], newCoords[1], 99999999)
                grid.addPoint(nextPoint)
                #print('left point at ', nextPoint.x, nextPoint.y, ' not visited. Facing', facingIdx)
                turned = True
                return directionInputs[leftIdx]
            elif leftPoint.data >= OPEN:
                nextPoint = leftPoint
                facingIdx = leftIdx
                #print('left point at ', nextPoint.x, nextPoint.y, ' is open. Facing', facingIdx)
                return directionInputs[leftIdx]
            
            # go straight
            newCoords = currentPoint + directions[facingIdx]
            forwardPoint = grid.getPoint(newCoords[0], newCoords[1])
            if forwardPoint is None:
                forwardPoint = com.Point(newCoords[0], newCoords[1], 99999999)
                grid.addPoint(forwardPoint)
                #print('fwd point at ', nextPoint.x, nextPoint.y, ' is empty. Facing', facingIdx)
            elif forwardPoint.data == WALL:
                facingIdx = (facingIdx - 1) % 4
                #print('fwd point at ', nextPoint.x, nextPoint.y, ' is Wall. Facing', facingIdx)
                continue
            else:
                pass
                #print('fwd point at ', nextPoint.x, nextPoint.y, ' is open. Facing', facingIdx)
            nextPoint = forwardPoint
            return directionInputs[facingIdx]
    
    def outputHandler(output):
        nonlocal grid, currentPoint, OPEN, WALL, nextPoint, turned, facingIdx, maxSteps
        localTurned = turned
        turned = False
        if output == WALL:
            nextPoint.data = output
            #print('Ran into wall at', nextPoint.x, nextPoint.y)
        elif output == 2:
            print('Oxygen system at ', nextPoint.x, nextPoint.y, 'with steps', currentPoint.data)
            for point in grid.getAllPoints():
                if point.data > WALL:
                    point.data = 9999999999999999999999999
            nextPoint.data = WALL # make it a wall so we don't go back
            currentPoint = nextPoint
            maxSteps = 0
            return
        elif output == OPEN:
            steps = currentPoint.data + 1
            if steps < nextPoint.data:
                nextPoint.data = steps
                if maxSteps < steps:
                    maxSteps = steps
            #print('Walked to', nextPoint.x, nextPoint.y, 'with steps', steps)
            if steps % 20 == 0:
                print(grid)
            if localTurned:
                facingIdx = (facingIdx + 1) % 4
            currentPoint = nextPoint

    compy = com.intCode(lines[0], False, None, inputHandler, outputHandler)
    compy.RunIntCodeComputer()