Beispiel #1
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)
Beispiel #2
0

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))
    expect(to_coord, 10, (2, -1))

    expect(storage_at, 1, 1)
    expect(storage_at, 2, 1)
    expect(storage_at, 3, 2)
Beispiel #3
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)
Beispiel #4
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)
Beispiel #5
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)
Beispiel #6
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)
Beispiel #7
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)
Beispiel #8
0
                elif grid[row][col + 1] != ' ':
                    direction = 'right'
                else:
                    raise f'Bad turn at {row}, {col}'
            else:
                if grid[row - 1][col] != ' ':
                    direction = 'up'
                elif grid[row + 1][col] != ' ':
                    direction = 'down'
                else:
                    raise f'Bad turn at {row}, {col}'
        elif char.isalpha():
            string += char
        elif char == ' ':
            return string, steps

        row_change, col_change = MOVES[direction]

        row += row_change
        col += col_change

        steps += 1

if __name__ == '__main__':
    data = [list(line) for line in input('19').split('\n')]

    string, steps = puzzle(data)

    solution(string, 'SXPZDFJNRL')
    solution(steps)
Beispiel #9
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)
Beispiel #10
0

def dance(moves, repeat=1, line='abcdefghijklmnop'):
    moves = list(map(parse, moves))

    line = list(line)
    seen_before = dict()
    i = 0

    while i < repeat:
        for cmd in moves:
            line = cmd(line)
        string = ''.join(line)
        if string in seen_before:

            i = repeat // i * i
        else:
            seen_before[string] = i
        i += 1
    return ''.join(line)


if __name__ == '__main__':
    example = ['s1', 'x3/4', 'pe/b']

    data = input('16').split(',')
    print(dance(example, line='abcde'))
    print(dance(example, repeat=2, line='abcde'))
    solution(dance(data), 'bkgcdefiholnpmja')
    solution(dance(data, repeat=1000000000))
Beispiel #11
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)
Beispiel #12
0
    i = 0
    skip_size = 0
    for round in range(0, rounds):
        for length in lengths:
            reverse_subset(array, i, length)
            i += length + skip_size
            skip_size += 1

    assert len(array) == 256
    dense_hash = [reduce(lambda a, b: a ^ b, array[i:i+16]) 
                  for i in range(0, 256, 16)]
    assert len(dense_hash) == 16
    print(dense_hash)
    hash = ''.join([format(n, '02x') for n in dense_hash])
    print(len(hash))
    assert len(hash) == 32
    print(hash)

    return array[0] * array[1]


if __name__ == '__main__':
    data = input('10')#.split(',')
    data = list(map(ord, data))

#    puzzle(list(map(ord, '')))

 #   print(puzzle([3, 4, 1, 5], 5))

    solution(puzzle(data))