Exemple #1
0
def _run_day(module: ModuleType, year: int, day: int, benchmarks: Benchmarks):
    """
    Runs all solutions in the given day
    """
    data = get_input_for_day(year, day)
    console.log(f"retrieved input for {year} {day:02}")

    try:
        benchmarks[year][day]["part one"] = getattr(module, "part_one")(data)
    except AttributeError:
        pass

    console.log(f"ran {year} {day:02} part one")

    try:
        benchmarks[year][day]["part two"] = getattr(module, "part_two")(data)
    except AttributeError:
        pass

    console.log(f"ran {year} {day:02} part two")

    for solution in _get_extra_solutions_in_module(module):
        readable_name = solution.replace("_", " ")
        try:
            benchmarks[year][day][readable_name] = getattr(module,
                                                           solution)(data)
        except AttributeError:
            pass

        console.log(f"ran {year} {day:02} {readable_name}")
Exemple #2
0
def test_get_input_for_day(mocker: pytest_mock.MockerFixture):
    mock_get_input = mocker.patch("adventofcode.util.input_helpers._get_input")
    mock_get_input.return_value = ["c0ffee", "cafe"]

    original_root_dir = input_helpers.ROOT_DIR
    input_helpers.ROOT_DIR = "dir"

    data = get_input_for_day(2020, 1)
    assert ["c0ffee", "cafe"] == data
    mock_get_input.assert_called_with(
        os.path.normpath("dir/inputs/2020/day_01.txt"))

    input_helpers.ROOT_DIR = original_root_dir
Exemple #3
0
def _run_day(module: ModuleType, year: int, day: int):
    """
    Runs all solutions in the given day
    """
    data = get_input_for_day(year, day)
    try:
        getattr(module, "part_one")(data)
    except AttributeError:
        pass

    try:
        getattr(module, "part_two")(data)
    except AttributeError:
        pass
Exemple #4
0
def verify_input_exists(year: int, day: int) -> None:
    try:
        _ = get_input_for_day(year, day)
        console.print(f"[yellow]Input data already exists for year {year} day {day}, skipping download")
        return
    except FileNotFoundError:
        try:
            get_input(year, day)
            console.print(f"Automatically downloaded input data for year {year} day {day}")
            return
        except HTTPError as e:
            console.print(f"[red]Could not retrieve input data for year {year} day {day} automatically: {e}")
        except FileNotFoundError:
            console.print(f"[red]Could not retrieve input data for year {year} day {day}: .session not set correctly")

    raise ValueError("unknown exception occurred in verify_input_exists")
                return nums[i:j + 1]
            if cur_sum > target:
                break

    return []


@solution_timer(2020, 9, 2)
def part_two(input_data: List[str], preamble_length: int = 25):
    nums = [int(x) for x in input_data]
    cur_pos = preamble_length

    while cur_pos < len(nums):
        if is_valid(nums[cur_pos - preamble_length:cur_pos], nums[cur_pos]):
            cur_pos += 1
        else:
            break

    cont_sum = find_continuous_sum(nums, nums[cur_pos])

    if len(cont_sum) == 0:
        raise SolutionNotFoundException(2020, 9, 2)

    return min(cont_sum) + max(cont_sum)


if __name__ == "__main__":
    data = get_input_for_day(2020, 9)
    part_one(data)
    part_two(data)
Exemple #6
0

def iterateBoard2(board, iterations):
    board[0][0] = "#"
    board[0][-1] = "#"
    board[-1][0] = "#"
    board[-1][-1] = "#"
    for i in range(iterations):
        board = nextStep2(board)
    return board


@solution_timer(2015, 18, 1)
def part_one(input_data: List[str]):
    input_data = [list(line) for line in input_data]
    p1 = iterateBoard(input_data, 100)
    return sum(lst.count("#") for lst in p1)


@solution_timer(2015, 18, 2)
def part_two(input_data: List[str]):
    input_data = [list(line) for line in input_data]
    p2 = iterateBoard2(input_data, 100)
    return sum(lst.count("#") for lst in p2)


if __name__ == "__main__":
    data = get_input_for_day(2015, 18)
    part_one(data)
    part_two(data)
                if x > 0 and keypad[y][x - 1] is not None:
                    x -= 1
            elif dir == "R":
                if x < len(keypad[0]) - 1 and keypad[y][x + 1] is not None:
                    x += 1
        code += str(keypad[y][x])
    return code


@solution_timer(2016, 2, 1)
def part_one(input_data: List[str]):
    return decipherCode(input_data)


@solution_timer(2016, 2, 2)
def part_two(input_data: List[str]):
    keypad = [
        [None, None, 1, None, None],
        [None, 2, 3, 4, None],
        [5, 6, 7, 8, 9],
        [None, "A", "B", "C", None],
        [None, None, "D", None, None],
    ]
    return decipherCode(input_data, keypad, 0, 2)


if __name__ == "__main__":
    data = get_input_for_day(2016, 2)
    part_one(data)
    part_two(data)
    for row in board:
        for num in row:
            if num > 1:
                count += 1
    return count


@solution_timer(2021, 5, 2)
def part_two(input_data: List[str]):
    lines: List[Tuple[Tuple[int, int], Tuple[int, int]]] = []
    for line in input_data:
        p1, p2 = line.split(" -> ")
        x1, y1 = p1.split(",")
        x2, y2 = p2.split(",")
        lines.append(((int(x1), int(y1)), (int(x2), int(y2))))

    board = map_lines(lines)
    # print_board(board)
    count = 0
    for row in board:
        for num in row:
            if num > 1:
                count += 1
    return count


if __name__ == "__main__":
    data = get_input_for_day(2021, 5)
    part_one(data)
    part_two(data)