Beispiel #1
0
def test_split_packets_operator():
    bin_str = "11101110000000001101010000001100100000100011000001100000"
    literal_1 = ValuePacket(2, 4, 1)
    literal_2 = ValuePacket(4, 4, 2)
    literal_3 = ValuePacket(1, 4, 3)
    expected = OperatorPacket(7, 3, [literal_1, literal_2, literal_3])
    assert split_packets(bin_str) == [expected]
Beispiel #2
0
def test_full(hex_str, expected):
    actual = sum_version_numbers(split_packets(hexes_to_bin(hex_str)))
    assert actual == expected
Beispiel #3
0
def test_split_packets_operator():
    bin_str = "00111000000000000110111101000101001010010001001000000000"
    literal_1 = ValuePacket(6, 4, 10)
    literal_2 = ValuePacket(2, 4, 20)
    expected = OperatorPacket(1, 6, [literal_1, literal_2])
    assert split_packets(bin_str) == [expected]
Beispiel #4
0
def test_split_packets_value():
    bin_str = "110100101111111000101000"
    expected = [ValuePacket(6, 4, 2021)]
    assert split_packets(bin_str) == expected
Beispiel #5
0
def test_full(hex_str, expected):
    packets = split_packets(hexes_to_bin(hex_str))
    actual = evaluate_packet(packets[0])
    assert actual == expected
Beispiel #6
0
        elif type_ == 3:
            # Maximum
            return max(values)
        elif type_ == 5:
            # Greater than
            if values[0] > values[1]:
                return 1
            return 0
        elif type_ == 6:
            # Less than
            if values[0] < values[1]:
                return 1
            return 0
        elif type_ == 7:
            # Equal to
            if values[0] == values[1]:
                return 1
            return 0
        else:
            raise ValueError("Unrecognised/unexpected packet type")
    else:
        raise ValueError("Unrecognised packet type")


if __name__ == "__main__":
    hex_str = load_input()
    bin_str = hexes_to_bin(hex_str)
    packets = split_packets(bin_str)
    answer = evaluate_packet(packets[0])
    print(answer)