Example #1
0
def generateChords(noteRange=(3, 4), maxWidth=1.999):
    for last in utils.integers(1):  # last harmonic present in a chord
        first = int(math.ceil(last / maxWidth))
        harms = range(first, last)  # harmonics to choose
        if len(harms) == 0: continue
        for count in range(noteRange[0] - 1, noteRange[1]):  # (3, 4) -> 2..3
            if len(harms) < count: continue
            for c in utils.combinations(harms, count):
                chord = c + (last, )
                if coprimes(*chord):
                    yield chord
Example #2
0
def main(lines):
    cards = deque(list(range(N)))
    for line in lines:
        if line.startswith('deal with increment'):
            x = int(integers(line)[0])
            print('increment', x)
            cards = increment(cards, x)
        elif line == 'deal into new stack':
            print('new stack')
            cards = new_stack(cards)
        elif line.startswith('cut'):
            x = int(integers(line)[0])
            print("cut", x)
            cards = cut(cards, x)
        else:
            raise Exception('problem')
        print(cards)

    for i, x in enumerate(cards):
        if x == 2019:
            print('position', i)
            break
Example #3
0
def part_1(data):
    mem_addr = {}
    for line in data.splitlines():
        if line.startswith('mask'):
            mask = line[7:]
        else:
            mem, num = utils.integers(line)
            num = bin(num)[2:].zfill(36)
            total = ''
            for index, char in enumerate(mask):
                if char.isdigit():
                    total += char
                else:
                    total += num[index]
            mem_addr[mem] = int(total, 2)
    return sum(mem_addr.values())
def part_2(rules: list, color: str) -> int:
    bags = {}
    for line in rules:
        colors = re.findall(r'(\w* \w*) bag', line)
        primary = colors[0]
        secondary = dict(zip(colors[1:], utils.integers(line)))
        bags[primary] = secondary

    def stack(bag):
        total = 1
        if bags[bag]:
            for inside in bags[bag]:
                total += bags[bag][inside] * stack(inside)
            return total
        return total

    return stack(color) - 1
def main():
    d = utils.integers(open('../inputs/15').read())
    print(part_1(d))
def main():
    d = integers(open('01').read())
    print(part_1(d))
    print(part_2(d))
def main():
    d = integers(open('../inputs/09').read())
    print(xmas(25, d))
    print(part_2(25, d))
Example #8
0
def main():
    d = open('../inputs/13').read().splitlines()
    timestamp = int(d[0])
    buses = utils.integers(d[1])

    print(part_1(timestamp, buses))