Exemple #1
0
        buffer.insert(i + 1, next_value)
        i += 1
        next_value += 1

    return buffer[i + 1]


def spinlock2(steps):
    buffer = [0]
    i = 0
    next_value = 1

    while next_value <= 50000000:
        i = (i + steps) % next_value
        if i == 0:
            if len(buffer) == 1:
                buffer.append(next_value)
            else:
                buffer[1] = next_value
        i += 1
        next_value += 1

    return buffer[1]


if __name__ == '__main__':
    expect(spinlock1, 3, 638)
    solution(spinlock1(394), 926)

    solution(spinlock2(394), 10150888)
Exemple #2
0
# - abcde xyz ecdab is not valid - the letters from the third word can be
#   rearranged to form the first word.
# - a ab abc abd abf abj is a valid passphrase, because all letters need to be
#   used when forming another word.
# - iiii oiii ooii oooi oooo is valid.
# - oiii ioii iioi iiio is not valid - any of these words can be rearranged to
#   form any other word.
def is_valid2(password):
    words = password.split(' ')
    seen = set()
    for word in words:
        word = ''.join(sorted(word))
        if word in seen:
            return False
        seen.add(word)
    return True


if __name__ == '__main__':
    passwords = input('04').split('\n')

    # Part 1
    expect(is_valid1, 'aa bb cc dd ee', True)
    expect(is_valid1, 'aa bb cc dd aa', False)
    solution(sum(map(is_valid1, passwords)), 455)

    # Part 2
    expect(is_valid2, 'abcde fghij', True)
    expect(is_valid2, 'abcde xyz ecdab', False)
    solution(sum(map(is_valid2, passwords)), 186)
Exemple #3
0
            regs[reg] = cin.pop(0)
            return i + 1
        else:
            return exec(cmd, args, regs, i)

    def is_blocked(i, channel):
        return (i >= len(instructions)
                or (instructions[i].startswith('rcv') and len(channel) == 0))

    counter = 0

    while True:
        if not is_blocked(i1, channel1):
            i1 = exec_each(i1, regs1, channel1, channel2)
        elif not is_blocked(i2, channel2):
            i2 = exec_each(i2, regs2, channel2, channel1)
            if instructions[i2].startswith('snd'):
                counter += 1
        else:
            return counter


if __name__ == '__main__':
    example1 = input('18-example').split('\n')
    data = input('18').split('\n')

    expect(duet, example1, 4)
    solution(duet(data), 3188)

    solution(communicate(data), 7112)
Exemple #4
0
                    (x - 1, y + 1), (x - 1, y + 0), (x - 1, y - 1),
                    (x + 0, y - 1), (x + 1, y - 1)]
        value = sum(grid.get(coord, 0) for coord in adjacent)
        grid[(x, y)] = value
        yield value


def storage_at(addr):
    return nth(storage(), addr - 1)


if __name__ == '__main__':
    number = 265149

    # Part 1
    expect(access, 1, 0)
    expect(access, 12, 3)
    expect(access, 21, 4)
    expect(access, 23, 2)
    expect(access, 1024, 31)
    solution(access(number), 438)

    # Part 2
    expect(to_coord, 1, (0, 0))
    expect(to_coord, 2, (1, 0))
    expect(to_coord, 3, (1, 1))
    expect(to_coord, 4, (0, 1))
    expect(to_coord, 5, (-1, 1))
    expect(to_coord, 20, (-2, -1))
    expect(to_coord, 22, (-1, -2))
    expect(to_coord, 25, (2, -2))
Exemple #5
0
        index, value = max(enumerate(data), key=operator.itemgetter(1))
        data[index] = 0
        while value > 0:
            index = (index + 1) % len(data)
            data[index] += 1
            value -= 1

        steps += 1
    return steps, steps - seen[str(data)]


def puzzle1(data):
    return puzzle(data[:])[0]


def puzzle2(data):
    return puzzle(data[:])[1]


if __name__ == '__main__':
    data = list(map(int, input('06').split('\t')))

    # Part 1
    expect(puzzle1, [0, 2, 7, 0], 5)
    solution(puzzle1(data), 12841)

    # Part 2
    expect(puzzle2, [0, 2, 7, 0], 4)
    solution(puzzle2(data), 8038)
Exemple #6
0
# largest value and the smallest value; the checksum is the sum of all of these
# differences.
def checksum(table):
    return sum(max(row) - min(row) for row in table)


# It sounds like the goal is to find the only two numbers in each row where one
# evenly divides the other - that is, where the result of the division
# operation is a whole number. They would like you to find those numbers on
# each line, divide them, and add up each line's result.
def divisible(table):
    return sum(next(a // b for a, b in permutations(row, 2) if a % b == 0)
               for row in table)


if __name__ == '__main__':
    table = [[int(s) for s in row.split('\t')]
             for row in input('02').split('\n')]

    # Part 1
    expect(checksum, [[5, 1, 9, 5],
                      [7, 5, 3],
                      [2, 4, 6, 8]], 18)
    solution(checksum(table), 42299)

    # Part 2
    expect(divisible, [[5, 9, 2, 8],
                       [9, 4, 7, 3],
                       [3, 8, 6, 5]], 9)
    solution(divisible(table), 277)
Exemple #7
0
    def get_weight(name):
        weight = weights[name]
        children = tree[name]
        children_weights = list(map(get_weight, children))
        wrong_index, right, wrong = odd_one_out(children_weights)
        if wrong is not None:
            wrong_child = weights[children[wrong_index]]
            right_child = wrong_child - wrong + right
            raise WrongWeightError(dict(wrong=wrong_child, right=right_child))
        return weight + sum(children_weights)

    try:
        get_weight(root)
        return None
    except WrongWeightError as error:
        return error.args[0]['right']


if __name__ == '__main__':
    example = input('07-example').split('\n')
    data = input('07').split('\n')

    # Part 1
    expect(puzzle1, example, 'tknk')
    solution(puzzle1(data), 'hlqnsbe')

    # Part 2
    expect(puzzle2, example, 60)
    solution(puzzle2(data), 1993)
Exemple #8
0

# Now, instead of considering the next digit, it wants you to consider the
# digit halfway around the circular list. That is, if your list contains 10
# items, only include a digit in your sum if the digit 10/2 = 5 steps forward
# matches it. Fortunately, your list has an even number of elements.
def captcha2(string):
    assert len(string) % 2 == 0 # Is even
    
    return sum(int(char) for char, next in pairwise(string, len(string)//2)
               if char == next)


if __name__ == '__main__':
    # Part 1
    string = input('01')

    expect(captcha1, '1122', 3)
    expect(captcha1, '1111', 4)
    expect(captcha1, '1234', 0)
    expect(captcha1, '91212129', 9)
    solution(captcha1(string), 1223)

    # Part 2
    expect(captcha2, '1212', 6)
    expect(captcha2, '1221', 0)
    expect(captcha2, '123425', 4)
    expect(captcha2, '123123', 12)
    expect(captcha2, '12131415', 4)
    solution(captcha2(string), 1284)
Exemple #9
0
# more, instead decrease it by 1. Otherwise, increase it by 1 as before.
def puzzle1(data, part_two=False):
    data = data[:]
    index = 0
    steps = 0
    while index >= 0 and index < len(data):
        value = data[index]
        if part_two and value >= 3:
            data[index] -= 1
        else:
            data[index] += 1
        index += value
        steps += 1
    return steps


def puzzle2(data):
    return puzzle1(data, part_two=True)


if __name__ == '__main__':
    data = list(map(int, input('05').split('\n')))

    # Part 1
    expect(puzzle1, [0, 3, 0, 1, -3], 5)
    solution(puzzle1(data), 326618)

    # Part 2
    expect(puzzle2, [0, 3, 0, 1, -3], 10)
    solution(puzzle2(data), 21841249)