Ejemplo n.º 1
0
        offset = 2
        childrenLength = 0
        self.children = []
        for n in range(self.childrenCount):
            self.children.append(Node(self.depth + 1))
            length = self.children[n].process(numbers[offset:])
            childrenLength += length
            offset += length

        # Find the list of metas
        metaBegin = 2 + childrenLength
        metaEnd = metaBegin + self.metasCount
        self.metas = numbers[metaBegin:metaEnd]

        # Return the length of this node in the numbers list
        return metaEnd

    def count(self):
        return sum([c.count() for c in self.children]) + sum(self.metas)


def solve(numbers):
    root = Node()
    root.process(numbers)
    return root.count()


if __name__ == "__main__":
    result = solve([int(n) for n in runner.start().split(" ")])
    runner.done(result)
Ejemplo n.º 2
0
    startIndex = 0

    for day in range(1,21):
        paddedPots = [False]*4 + pots + [False]*4
        startIndex -= 4
        newPots = [False] * len(paddedPots)

        # Find any matching patterns
        for rule in rules:
            for index in range(len(paddedPots) - 5):
                if rule[0] == paddedPots[index:index+5]:
                    newPots[index+2] = rule[1]

        startIndex += trimStart(newPots)
        pots = newPots[trimStart(newPots):len(newPots)-trimEnd(newPots)]

        str = "".join(["#" if p else "." for p in pots])
        print(f"{day}: {str}")

    sum = 0
    for index, pot in enumerate(pots):
        if pot:
            sum += index + startIndex

    return sum

if __name__ == "__main__":
    result = solve(runner.start().splitlines())
    runner.done(result)
Ejemplo n.º 3
0
    mem[(x,y)] = power

    return power

def value(serial, xs, ys, size):
    sum = 0
    for x in range(xs, min(xs+size, 301)):
        for y in range(ys, min(301, ys+size)):
            sum += power(serial, x, y)

    return sum

def solve(serial):
    maxS = 0
    maxPos = (0,0,0)
    for size in range(1, 100):
        for x in range(1,298+1):
            for y in range(1, 299):
                val = value(serial, x, y, size)
                if val > maxS:
                    maxS = val
                    maxPos=(x,y,size)
        print(f"done size {size} {maxPos} {maxS}")

    return f"{maxPos[0]},{maxPos[1]},{maxPos[2]}"


if __name__ == "__main__":
    result = solve(int(runner.start()))
    runner.done(result)