Ejemplo n.º 1
0
def test_get_numbers():
    expected = [
        35,
        20,
        15,
        25,
        47,
        40,
        62,
        55,
        65,
        95,
        102,
        117,
        150,
        182,
        127,
        219,
        299,
        277,
        309,
        576,
    ]
    assert get_numbers(EXAMPLE_CYPHER_PATH) == expected
Ejemplo n.º 2
0
def test_solver():
    example_numbers = get_numbers(EXAMPLE_CYPHER_PATH)
    assert solver(example_numbers, 5) == 127
def test_solver():
    example_numbers = get_numbers(EXAMPLE_CYPHER_PATH)
    with patch("day_9.xmas_cypher_pt2.solver_pt1", return_value=127):
        assert solver_pt2(example_numbers) == 62
Ejemplo n.º 4
0

def sum_min_max(numbers: Iterable[int]) -> int:
    """
    calculated the sum of the largest value and smallest value in a given list of numbers
    :param numbers: list of numbers
    :return: value of min + max
    """
    return min(numbers) + max(numbers)


def solver_pt2(cypher: List[int]) -> int:
    """
    find the contiguous range of numbers which sum to the value found in part 1, and return min + max
    :param cypher: ordered list of numbers
    :return: min + max for the contiguous range of numbers summing to the target value
    """
    target_sum = solver_pt1(cypher)
    n_numbers = len(cypher)
    for index in range(n_numbers):
        max_window = n_numbers - index
        for window_length in range(max_window):
            window = get_window(cypher, index, window_length)
            if is_sum(window, target_sum):
                return sum_min_max(window)
    raise NoSolutionError


if __name__ == "__main__":
    print(solver_pt2(get_numbers()))
Ejemplo n.º 5
0
import os
from itertools import combinations
from typing import List

from conf import ROOT_DIR
from day_9.utils import NoSolutionError, get_numbers, get_window, is_sum

CYPHER_PATH = os.path.join(ROOT_DIR, "day_9/xmas_cypher.csv")


def solver(cypher: List[int], preamble_length: int = 25) -> int:
    """
    find the first number for which no pair of numbers in the preamble sum to its value
    :param cypher: ordered list of numbers
    :param preamble_length: length of the preceeding list of numbers
    :return: value of the first number which breaks the rule
    """
    for index in range(len(cypher)):
        window = get_window(cypher, index, preamble_length + 1)
        current_number = window[-1]
        preamble = set(window[:-1])
        if any([is_sum(pair, current_number) for pair in combinations(preamble, 2)]):
            pass
        else:
            return current_number
    raise NoSolutionError()


if __name__ == "__main__":
    print(solver(get_numbers()))