def test_iterate(self) -> None:
        rule_lines = common.Lines(
            textwrap.dedent('''\
                0: 4 1 5
                1: 2 3 | 3 2
                2: 4 4 | 5 5
                3: 4 5 | 5 4
                4: "a"
                5: "b"''').splitlines())

        rule_trees = day_19_monster_messages.parse_rules(lines=rule_lines)

        identifier_to_node_types = dict()  # type: Dict[int, List[str]]

        for identifier, rule_tree in rule_trees.items():
            node_types = [
                re.sub(r"^Node", "",
                       type(node).__name__).replace("Sequence", "Seq").replace(
                           "Reference", "Ref").replace("Literal", "Lit")
                for node in day_19_monster_messages.iterate(
                    rule_tree=rule_tree)
            ]

            identifier_to_node_types[identifier] = node_types

        # fmt: off
        self.assertDictEqual(
            {
                0: ['Seq', 'Ref', 'Ref', 'Ref'],
                1: ['Or', 'Seq', 'Ref', 'Ref', 'Seq', 'Ref', 'Ref'],
                2: ['Or', 'Seq', 'Ref', 'Ref', 'Seq', 'Ref', 'Ref'],
                3: ['Or', 'Seq', 'Ref', 'Ref', 'Seq', 'Ref', 'Ref'],
                4: ['Lit'],
                5: ['Lit']
            }, identifier_to_node_types)
    def test_example_short(self) -> None:
        lines = common.Lines(
            textwrap.dedent("""\
                16
                10
                15
                5
                1
                11
                7
                19
                6
                12
                4""").splitlines())

        adapters = day_10_adapter_array.parse(lines)

        histo = day_10_adapter_array.histogram_differences(adapters)

        self.assertEqual(7, histo[1])
        self.assertEqual(5, histo[3])

        result = day_10_adapter_array.compute_result(histo)

        self.assertEqual(7 * 5, result)
    def test_repr(self) -> None:
        rule_lines = common.Lines(
            textwrap.dedent('''\
                0: 4 1 5
                1: 2 3 | 3 2
                2: 4 4 | 5 5
                3: 4 5 | 5 4
                4: "a"
                5: "b"''').splitlines())

        rules_to_repr = dict()  # type: Dict[int, str]

        rule_trees = day_19_monster_messages.parse_rules(lines=rule_lines)

        for identifier, rule_tree in rule_trees.items():
            rules_to_repr[identifier] = day_19_monster_messages.repr_rule_tree(
                rule_tree=rule_tree)

        # fmt: off
        self.assertDictEqual(
            {
                0: 'Seq(Ref(4) Ref(1) Ref(5))',
                1: 'Or(Seq(Ref(2) Ref(3)) | Seq(Ref(3) Ref(2)))',
                2: 'Or(Seq(Ref(4) Ref(4)) | Seq(Ref(5) Ref(5)))',
                3: 'Or(Seq(Ref(4) Ref(5)) | Seq(Ref(5) Ref(4)))',
                4: 'Lit("a")',
                5: 'Lit("b")'
            }, rules_to_repr)
Пример #4
0
    def test_case(self) -> None:
        rule_lines = common.Lines(
            textwrap.dedent("""\
                class: 1-3 or 5-7
                row: 6-11 or 33-44
                seat: 13-40 or 45-50""").splitlines())

        rule_parsings = day_16_ticket_translation.parse_rules(lines=rule_lines)

        # fmt: off
        rules = [
            day_16_ticket_translation.Rule(identifier=parsing.identifier,
                                           ranges=parsing.ranges)
            for parsing in rule_parsings
        ]
        # fmt: on

        nearby_ticket_lines = textwrap.dedent("""\
            7,3,47
            40,4,50
            55,2,20
            38,6,12""").splitlines()

        nearby_tickets = day_16_ticket_translation.parse_nearby_tickets(
            lines=nearby_ticket_lines)

        invalid_values = day_16_ticket_translation.list_all_invalid_values(
            rules=rules, tickets=nearby_tickets)

        self.assertListEqual([4, 55, 12], invalid_values)

        error_rate = day_16_ticket_translation.compute_error_rate(
            invalid_values=invalid_values)
        self.assertEqual(71, error_rate)
Пример #5
0
    def test_case(self) -> None:
        lines = common.Lines(
            textwrap.dedent("""\
                mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
                mem[8] = 11
                mem[7] = 101
                mem[8] = 0""").splitlines())

        program = day_14_docking_data.parse_lines(lines=lines)

        memory = day_14_docking_data.execute(program=program)
        self.assertDictEqual({7: 101, 8: 64}, memory.slots)  # type: ignore

        self.assertEqual(165, day_14_docking_data.sum_memory(memory=memory))
Пример #6
0
    def test_on_example(self) -> None:
        text = textwrap.dedent("""\
            light red bags contain 1 bright white bag, 2 muted yellow bags.
            dark orange bags contain 3 bright white bags, 4 muted yellow bags.
            bright white bags contain 1 shiny gold bag.
            muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.
            shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags.
            dark olive bags contain 3 faded blue bags, 4 dotted black bags.
            vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.
            faded blue bags contain no other bags.
            dotted black bags contain no other bags.""")

        lines = common.Lines(text.splitlines())

        self.assertEqual(4, day_7_handy_haversacks.count_containers(lines))
    def test_example_long(self) -> None:
        lines = common.Lines(
            textwrap.dedent("""\
                28
                33
                18
                42
                31
                14
                46
                20
                48
                47
                24
                23
                49
                45
                19
                38
                39
                11
                1
                32
                25
                35
                8
                17
                7
                9
                4
                2
                34
                10
                3""").splitlines())

        adapters = day_10_adapter_array.parse(lines)

        histo = day_10_adapter_array.histogram_differences(adapters)

        self.assertEqual(22, histo[1])
        self.assertEqual(10, histo[3])

        result = day_10_adapter_array.compute_result(histo)

        self.assertEqual(22 * 10, result)
Пример #8
0
    def test_case(self) -> None:
        lines = common.Lines(
            textwrap.dedent("""\
                nop +0
                acc +1
                jmp +4
                acc +3
                jmp -3
                acc -99
                acc +1
                jmp -4
                acc +6
                """).splitlines())

        instructions = day_8_handheld_halting.parse(lines=lines)

        acc = day_8_handheld_halting.execute_instructions(
            instructions=instructions)

        self.assertEqual(5, acc)
    def test_case(self) -> None:
        rule_lines = common.Lines(
            textwrap.dedent('''\
                0: 4 1 5
                1: 2 3 | 3 2
                2: 4 4 | 5 5
                3: 4 5 | 5 4
                4: "a"
                5: "b"''').splitlines())

        rule_trees = day_19_monster_messages.parse_rules(lines=rule_lines)

        rule_0 = day_19_monster_messages.interpret_rule_0(
            rule_trees=rule_trees)

        for should_match in ["ababbb", "abbbab"]:
            remaining_suffix = rule_0.match(text=should_match)
            self.assertEqual("", remaining_suffix)

        for should_remain in ["bababa", "aaabbb", "aaaabbb"]:
            remaining_suffix = rule_0.match(text=should_remain)
            self.assertNotEqual("", remaining_suffix)

        for should_not_match in ["xyz", "xaaabbb"]:
            remaining_suffix = rule_0.match(text=should_not_match)
            self.assertIsNone(remaining_suffix)

        messages = textwrap.dedent("""\
            ababbb
            bababa
            abbbab
            aaabbb
            aaaabbb""").splitlines()

        count = day_19_monster_messages.count_matching_messages(
            rule_0=rule_0, messages=messages)

        self.assertEqual(2, count)