Example #1
0
    def test_answer_decode(self):
        form = CustomsForm("abcdf")
        assert form.answers["a"] is True
        assert form.answers["b"] is True
        assert form.answers["e"] is False
        assert form.answers["z"] is False

        form2 = CustomsForm("yz")
        for key, answer in form2.answers.items():
            if key == "y" or key == "z":
                assert answer is True
            else:
                assert answer is False
Example #2
0
 def test_no_answer_count(self):
     assert CustomsForm("a").no_count == 25
     assert CustomsForm("").no_count == 26
     assert CustomsForm("ad").no_count == 24
     assert CustomsForm("abcdefghijklmnopqrstuvwxyz").no_count == 0
     assert CustomsForm("aaaaaaaaaaaaaaaaaaaaaaaaaa").no_count == 25
     assert CustomsForm("abb").no_count == 24
     assert CustomsForm("abc").no_count == 23
     assert CustomsForm("ap").no_count == 24
Example #3
0
 def test_yes_answer_count(self):
     assert CustomsForm("a").yes_count == 1
     assert CustomsForm("").yes_count == 0
     assert CustomsForm("ad").yes_count == 2
     assert CustomsForm("abcdefghijklmnopqrstuvwxyz").yes_count == 26
     assert CustomsForm("aaaaaaaaaaaaaaaaaaaaaaaaaa").yes_count == 1
     assert CustomsForm("abb").yes_count == 2
     assert CustomsForm("abc").yes_count == 3
     assert CustomsForm("ap").yes_count == 2
 def test_equality(self):
     assert not CustomsGroup([]) == 12
     assert CustomsGroup([CustomsForm("abc")]) == \
         CustomsGroup([CustomsForm("abc")])
     assert CustomsGroup([CustomsForm("ab")]) == \
         CustomsGroup([CustomsForm("ab")])
     assert not CustomsGroup([CustomsForm("ab")]) == CustomsGroup(
         [CustomsForm("abc")]
     )
     assert not CustomsGroup([CustomsForm("abc")]) == CustomsGroup(
         [CustomsForm("ab")]
     )
 def from_str(cls, groups_encoded_forms):
     forms = []
     for line in groups_encoded_forms.split("\n"):
         forms.append(CustomsForm(line.strip()))
     return cls(forms)
    def test_common_count(self):
        groups_from_str = CustomsGroup.multiple_from_str(self.group_of_one_str + "\n\n" + self.group_of_three_str)
        assert sum_common_yes_answers_for_multiple_groups(groups_from_str) == 1
        assert sum_common_yes_answers_for_multiple_groups([self.group_of_one]) == 1
        assert sum_common_yes_answers_for_multiple_groups([self.group_of_three]) == 0

        group1 = CustomsGroup([CustomsForm("ovuxdgiheszjbaltw"), CustomsForm("oxwjiubhfylzavst")])
        group2 = CustomsGroup([CustomsForm("abcdefgxyz"), CustomsForm("abcdefg")])
        group3 = CustomsGroup([CustomsForm("abcdefgxyz"), CustomsForm("abcdfg")])
        group4 = CustomsGroup([CustomsForm("abcdefgxyz"), CustomsForm("acdfg"), CustomsForm("acdf")])
        group5 = CustomsGroup([CustomsForm("abcdefgxyz"), CustomsForm("acdfg"), CustomsForm("acdf"), CustomsForm("y")])
        assert group1.common_yes_count == 14
        assert group2.common_yes_count == 7
        assert group3.common_yes_count == 6
        assert group4.common_yes_count == 4
        assert group5.common_yes_count == 0

        assert sum_common_yes_answers_for_multiple_groups([group1, group2, group3, group4, group5]) == 31
class TestCustomsGroup:
    group_of_one_str = "a"
    group_of_three_str = "abc\nbc\nqwerty"

    group_of_three = CustomsGroup(
        [CustomsForm("abc"), CustomsForm("bc"), CustomsForm("qwerty")]
    )

    group_of_one = CustomsGroup([CustomsForm("a")])

    def test_form_count(self):
        assert self.group_of_three.size == 3
        assert self.group_of_one.size == 1

    def test_yes_count(self):
        assert self.group_of_three.yes_count == 11
        assert self.group_of_one.yes_count == 1

    def test_unique_yes_count(self):
        assert self.group_of_three.unique_yes_count == 9
        assert self.group_of_one.unique_yes_count == 1

    def test_common_yes_count(self):
        assert self.group_of_one.common_yes_count == 1
        assert self.group_of_three.common_yes_count == 0

    def test_no_count(self):
        assert self.group_of_one.no_count == 25
        assert self.group_of_three.no_count == 67

    def test_equality(self):
        assert not CustomsGroup([]) == 12
        assert CustomsGroup([CustomsForm("abc")]) == \
            CustomsGroup([CustomsForm("abc")])
        assert CustomsGroup([CustomsForm("ab")]) == \
            CustomsGroup([CustomsForm("ab")])
        assert not CustomsGroup([CustomsForm("ab")]) == CustomsGroup(
            [CustomsForm("abc")]
        )
        assert not CustomsGroup([CustomsForm("abc")]) == CustomsGroup(
            [CustomsForm("ab")]
        )

    def test_create_from_string(self):
        assert CustomsGroup.from_str(self.group_of_one_str) == \
            self.group_of_one
        assert CustomsGroup.from_str(self.group_of_three_str) == \
            self.group_of_three

    def test_create_multiple_from_string(self):
        assert CustomsGroup.multiple_from_str(
            self.group_of_one_str + "\n\n" + self.group_of_three_str
        ) == [self.group_of_one, self.group_of_three]

    def test_unique_count(self):
        groups = CustomsGroup.multiple_from_str(self.group_of_one_str + "\n\n" + self.group_of_three_str)
        assert sum_unique_yes_answers_for_multiple_groups(groups) == 10
        assert sum_unique_yes_answers_for_multiple_groups([self.group_of_one]) == 1
        assert sum_unique_yes_answers_for_multiple_groups([self.group_of_three]) == 9

    def test_common_count(self):
        groups_from_str = CustomsGroup.multiple_from_str(self.group_of_one_str + "\n\n" + self.group_of_three_str)
        assert sum_common_yes_answers_for_multiple_groups(groups_from_str) == 1
        assert sum_common_yes_answers_for_multiple_groups([self.group_of_one]) == 1
        assert sum_common_yes_answers_for_multiple_groups([self.group_of_three]) == 0

        group1 = CustomsGroup([CustomsForm("ovuxdgiheszjbaltw"), CustomsForm("oxwjiubhfylzavst")])
        group2 = CustomsGroup([CustomsForm("abcdefgxyz"), CustomsForm("abcdefg")])
        group3 = CustomsGroup([CustomsForm("abcdefgxyz"), CustomsForm("abcdfg")])
        group4 = CustomsGroup([CustomsForm("abcdefgxyz"), CustomsForm("acdfg"), CustomsForm("acdf")])
        group5 = CustomsGroup([CustomsForm("abcdefgxyz"), CustomsForm("acdfg"), CustomsForm("acdf"), CustomsForm("y")])
        assert group1.common_yes_count == 14
        assert group2.common_yes_count == 7
        assert group3.common_yes_count == 6
        assert group4.common_yes_count == 4
        assert group5.common_yes_count == 0

        assert sum_common_yes_answers_for_multiple_groups([group1, group2, group3, group4, group5]) == 31


    def test_count_unique_from_test_file(self):
        assert count_unique_yes_answers_in_file("day6/data/testdata.txt") == 11
        assert count_unique_yes_answers_in_file("day6/data/input.txt") == 6534

    def test_count_common_from_test_file(self):
        assert count_common_yes_answers_in_file("day6/data/testdata.txt") == 6
        assert count_common_yes_answers_in_file("day6/data/testdata_custom.txt") == 32
        assert count_common_yes_answers_in_file("day6/data/input.txt") == 3402
Example #8
0
 def test_equality(self):
     assert CustomsForm("abc") == CustomsForm("abc")
     assert CustomsForm("ab") == CustomsForm("ab")
     assert CustomsForm("z") == CustomsForm("z")
     assert not CustomsForm("abc") == CustomsForm("ab")
     assert not CustomsForm("abc") == 12
Example #9
0
 def test_empty_input(self):
     form = CustomsForm("")
     assert len(form.answers) == 26
     for answer in form.answers.values():
         assert answer is False