コード例 #1
0
def test_run_diagnostic(mock_input, mock_print):
    part1.intcode(get_input())
    # should get input once
    assert mock_input.call_count == 1
    # Every diagnostic except the last should be 0
    assert len(mock_print.call_args_list) > 1
    for call in mock_print.call_args_list[:-1]:
        assert call == mock.call(0) or call == mock.call("0")
    assert mock_print.call_args_list[-1] != mock.call(0)
    assert mock_print.call_args_list[-1] != mock.call("0")
コード例 #2
0
def test_get_input():
    input_data = """#########
#[email protected]#"""
    with patch("builtins.open", mock_open(read_data=input_data)):
        actual = get_input()
    expected = [
        ["#", "#", "#", "#", "#", "#", "#", "#", "#"],
        ["#", "b", ".", "A", ".", "@", ".", "a", "#"],
    ]
    assert actual == expected
コード例 #3
0
def main():
    phase_input = get_input()
    message_location = int(''.join(str(digit) for digit in phase_input[:7]))
    phase_input = phase_input * 10000
    num_phases = 100
    for phase in range(num_phases):
        print(f'Calculating digits for phase {phase} of {num_phases}')
        start = datetime.now()
        phase_input = calculate_phase(phase_input)
        end = datetime.now()
        duration = end - start
        print(f'Duration of calculate_phase for phase {phase} of '
              f'{num_phases}: {duration.total_seconds()} seconds')
    return phase_input[message_location:message_location + 8]
コード例 #4
0
def test_get_input_type():
    program = get_input()
    for item in program:
        assert isinstance(item, int)
コード例 #5
0
        if int(digit) > int(password[i + 1]):
            not_decreasing = False
            break

    return not_decreasing


def is_valid_password(password: str) -> bool:
    # Note - the tests use this function name - update them if you rename this
    # two adjacent digits are the same

    return does_repeat(password) and not_decreasing(password)


def count_possible_passwords(min_: str, max_: str) -> int:

    count = 0
    for password in range(int(min_), int(max_) + 1):
        if is_valid_password(str(password)):
            print(password)
            count += 1

    return count


if __name__ == "__main__":
    input_ = get_input()
    print(input_)
    result = count_possible_passwords(input_.min, input_.max)
    print(result)
コード例 #6
0

def find_settings(program: List[int]) -> Tuple[List[int], int]:
    """
    Return combination of phases to maximise the output of the chain of
    amplifiers.

    TODO: Evaluate if this belongs in day 7 only, or should be part of the
    common computer code.

    Finds which sequence of phases (5, 6, 7, 8, 9), results in the largest
    signal from the amplifier chain.  See run_feedback_loop for details of the
    amplifiers, and how the phase is interpreted.

    """
    max_value = 0
    max_setting = None
    for setting in permutations(range(5, 10)):
        result = run_feedback_loop(program, setting)
        if result > max_value:
            max_value = result
            max_setting = setting

    return (list(max_setting), max_value)


if __name__ == "__main__":
    input_data = get_input()
    settings = find_settings(input_data)
    print(settings)
コード例 #7
0
def main():
    # temporary input (make parsing function later)
    raw_input = get_input()
    reactions = parse_input(raw_input)
    ore = calculate_total_ore(reactions)
    print(f'{ore} ore required')
コード例 #8
0
def main():
    input_data = get_input()
    return count_steps(input_data)
コード例 #9
0
def test_get_input():
    input_data = '12345678'
    with patch("builtins.open", mock_open(read_data=input_data)):
        actual = get_input()
    expected = [1, 2, 3, 4, 5, 6, 7, 8]
    assert actual == expected
コード例 #10
0
def part2():
    input_ = get_input()
    return decode_image(input_, 25, 6).replace('0', ' ')
コード例 #11
0
def part1():
    input_ = get_input()
    return meaningless_calculation(input_, 25, 6)