Exemplo n.º 1
0
def part_one(filename: str) -> int:
    public_keys = read_integers(get_path(__file__, filename))
    pub_key1, pub_key2 = public_keys[0], public_keys[1]
    loop1, loop2 = get_loop_size(pub_key1), get_loop_size(pub_key2)
    encryption_key = transform(pub_key1, loop2)
    assert (encryption_key == transform(pub_key2, loop1))
    return encryption_key
Exemplo n.º 2
0
def part_two(filename: str) -> int:
    ints = read_integers(get_path(__file__, filename))
    # use twosum solution from p1 to reduce time complexity from
    # O(n^3) to O(n^2)
    for i, n in enumerate(ints):
        multiplier = two_sum(ints[i + 1:], 2020 - n)
        if multiplier != -1:
            return n * multiplier
    return -1
Exemplo n.º 3
0
def part_one(filename: str) -> int:
    ints = read_integers(get_path(__file__, filename))
    ints.sort()
    ones, threes, v = 0, 0, 0

    for x in ints:
        threes += (x - v == 3)
        ones += (x - v == 1)
        v = x

    return ones * (threes + 1)
Exemplo n.º 4
0
def part_two_2(filename: str) -> int:
    ints = read_integers(get_path(__file__, filename))
    ints_set = set(ints)
    ints.sort()
    ways = [0] * (max(ints) + 1)

    for x in [1, 2, 3]:
        ways[x] = int(x in ints)

    for i in range(2, max(ints) + 1):
        if i in ints_set:
            ways[i] += ways[i - 1] + ways[i - 2] + ways[i - 3]

    return ways[-1]
Exemplo n.º 5
0
def part_two_1(filename: str) -> int:
    ints = read_integers(get_path(__file__, filename))
    ints_set = set(ints)
    cache = [-1] * max(ints)

    def number_ways(v):
        if v not in ints_set:
            return 0
        if v == max(ints):
            return 1

        if cache[v] == -1:
            cache[v] = sum([number_ways(v + x) for x in range(1, 4)])

        return cache[v]

    return sum([number_ways(x) for x in range(1, 4)])
Exemplo n.º 6
0
def part_one(filename: str) -> int:
    ints = read_integers(get_path(__file__, filename))
    return two_sum(ints, 2020)
Exemplo n.º 7
0
def part_one(filename: str, preamble: int) -> int:
    ints = read_integers(get_path(__file__, filename))
    for i in range(preamble, len(ints)):
        if not can_sum(ints[i], ints[i - preamble:i + 1]):
            return ints[i]
    return -1
Exemplo n.º 8
0
def part_two(filename: str, preamble: int) -> int:
    ints = read_integers(get_path(__file__, filename))
    n = part_one(filename, preamble)
    return sub_array_sum(n, ints)