Ejemplo n.º 1
0
def process_text(input):
    rules_text, yours_text, nearby_text = input.strip().split('\n\n')
    yours = util.ints(yours_text)
    nearby = [util.ints(x) for x in nearby_text.splitlines()[1:]]
    rules = {}
    for line in rules_text.splitlines():
        field, values = line.split(': ')
        options = values.split(' or ')
        rules[field] = options
    return rules, yours, nearby
Ejemplo n.º 2
0
def process(input):
    in_paren = False
    in_marker = False
    marker = ''
    num_chars = -1
    data = []
    for i, x in enumerate(input):
        if num_chars > 0:
            data.append(x)
            num_chars -= 1
            continue

        if x == '(':
            in_paren = True
            if not in_marker:
                in_marker = True

        if in_marker:
            marker += x
        else:
            data.append(x)

        if x == ')':
            in_paren = False
            if in_marker:
                data.append(marker)
                num_chars, repeat = util.ints(marker)
                in_marker = False
                marker = ''

    # print(data)

    output = ''
    num_chars = -1
    char_cnt = 0
    char_group = ''
    for x in data:
        if len(x) > 1:
            num_chars, repeat = util.ints(x)
            char_cnt = 0
            char_group = ''
        elif num_chars > char_cnt:
            char_group += x
            char_cnt += 1
            if num_chars == char_cnt:
                for i in range(repeat):
                    output += char_group
                num_chars = -1
        else:
            output += x

    # print(output)
    # print()
    return output
Ejemplo n.º 3
0
def shuffle(input, deck):
    for line in input.strip().splitlines():
        if 'new' in line:
            deck = deal_new(deck)
        if 'increment' in line:
            n = util.ints(line)[0]
            deck = deal_inc(deck, n)
        if 'cut' in line:
            n = util.ints(line)[0]
            deck = cut(deck, n)
        print(line)
        print('  ', deck)
    return deck
Ejemplo n.º 4
0
def shuffle(input, size, index):
    for line in reversed(input.strip().splitlines()):
        if 'new' in line:
            index = deal_new(size, index)
        if 'increment' in line:
            n = util.ints(line)[0]
            index = deal_inc(size, index, n)
        if 'cut' in line:
            n = util.ints(line)[0]
            index = cut(size, index, n)
        # print(line)
        # print('  ', index)
    return index
Ejemplo n.º 5
0
def process(input, preamble_len):
    preamble = []
    for num in util.ints(input):
        if len(preamble) == preamble_len:
            # print(preamble, num)
            if not is_valid(preamble, num):
                return num
            preamble = preamble[1:]
        preamble.append(num)
Ejemplo n.º 6
0
def find_contig(input, target_sum):
    current_set = []
    input = util.ints(input)
    for i_start in range(0, len(input)):
        for i_end in range(i_start, len(input)):
            # print(i_start, i_end)
            nums = input[i_start:i_end]
            # print(nums, sum(nums))
            if sum(nums) == target_sum:
                return min(nums) + max(nums)
Ejemplo n.º 7
0
def num_arrangements(input):
    adapters = util.ints(input)
    adapters = adapters + [max(adapters) + 3]
    adapters = sorted(adapters)
    print(adapters)

    global cache
    cache = {}
    num_paths = get_valid_paths(adapters, 0)
    print('total', num_paths)
    return num_paths
Ejemplo n.º 8
0
def process(input):
    instrs = input.strip().splitlines()
    mask = None
    mem = {}
    for instr in instrs:
        cmd, value = instr.split(' = ')
        if cmd == 'mask':
            mask = value
        else:
            pos = util.ints(cmd)[0]
            masked_value = get_masked_value(mask, value)
            mem[pos] = masked_value
    total = sum([v for v in mem.values()])
    return total
Ejemplo n.º 9
0
def shuffle(input, size, iterations, index):
    increment_mul = 1
    offset_diff = 0

    for line in input.strip().splitlines():
        if 'new' in line:
            increment_mul *= -1
            offset_diff += increment_mul
        if 'increment' in line:
            n = util.ints(line)[0]
            increment_mul *= pow(int(n), size - 2, size)
        if 'cut' in line:
            n = util.ints(line)[0]
            offset_diff += int(n) * increment_mul
    increment_mul %= size
    offset_diff %= size

    increment = pow(increment_mul, iterations, size)
    offset = offset_diff * (1 - increment) * pow(
        (1 - increment_mul) % size, size - 2, size)
    offset %= size

    card = (offset + index * increment) % size
    return card
Ejemplo n.º 10
0
def connect_all(input):
    adapters = util.ints(input)
    adapters.append(max(adapters) + 3)
    adapters = sorted(adapters)
    print(adapters)

    deltas = {}
    source = 0
    for adapter in adapters:
        diff = adapter - source
        source = adapter
        if diff not in deltas:
            deltas[diff] = 0
        deltas[diff] += 1
    # print(deltas)
    return deltas
Ejemplo n.º 11
0
def test():
    deck = [x for x in range(10)]

    assert deal_new(deck) == util.ints('9 8 7 6 5 4 3 2 1 0')
    assert cut(deck, 3) == util.ints('3 4 5 6 7 8 9 0 1 2')
    assert cut(deck, -4) == util.ints('6 7 8 9 0 1 2 3 4 5')
    assert deal_inc(deck, 3) == util.ints('0 7 4 1 8 5 2 9 6 3')

    assert shuffle(r"""
deal with increment 7
deal into new stack
deal into new stack
    """, deck) == util.ints('0 3 6 9 2 5 8 1 4 7')

    assert shuffle(r"""
cut 6
deal with increment 7
deal into new stack    
    """, deck) == util.ints('3 0 7 4 1 8 5 2 9 6')

    assert shuffle(r"""
deal with increment 7
deal with increment 9
cut -2    
    """, deck) == util.ints('6 3 0 7 4 1 8 5 2 9')

    assert shuffle(r"""
deal into new stack
cut -2
deal with increment 7
cut 8
cut -4
deal with increment 7
cut 3
deal with increment 9
deal with increment 3
cut -1    
    """, deck) == util.ints('9 2 5 8 1 4 7 0 3 6')
    exit(0)
Ejemplo n.º 12
0
def process(input, count):
    nums = util.ints(input)
    ages = {}
    last_num = None
    for i, num in enumerate(nums):
        ages[num] = [i + 1]
        last_num = num
    print(ages)
    for i in range(len(nums) + 1, count + 1):
        if last_num in ages and len(ages[last_num]) > 1:
            num = ages[last_num][0] - ages[last_num][1]
        else:
            num = 0
        if num in ages:
            ages[num] = [i, ages[num][0]]
        else:
            ages[num] = [i]
        # if i%1000000 == 0 or i == count+1:
        #     print('Turn', i, ':', num)
        #     print(ages)
        last_num = num
    return last_num
Ejemplo n.º 13
0
    for m in moons:
        m.reset()

    # Part 2
    num_steps = sim_moons_state(moons)
    print('Num steps to repeat:', num_steps)
    assert num_steps == 2772

    exit(0)


# test()

with open('input.txt', 'r') as f:
    input = [util.ints(x) for x in f.readlines()]
    temp = input.copy()
    moons = []
    for i, coords in enumerate(input):
        m = Moon(i, coords, [0, 0, 0])
        moons.append(m)
    print(*moons, sep='\n')

    # Part 1
    energy = sim_moons_steps(moons, 1000)
    print('Energy:', energy)
    assert energy == 7988

    for m in moons:
        m.reset()
Ejemplo n.º 14
0
def text_to_pairs(text):
    dirs = util.words(text)
    steps = util.ints(text)
    pairs = list(zip(dirs, steps))
    return pairs