Exemple #1
0
def draw(width: int, height: int) -> Lines:
    """Draw the pattern of the size ``width`` x ``height`` and return the text lines."""
    result = []  # type: List[str]

    vertical_pad = int(height / 5) - 1

    result.append("-" * width)

    for _ in range(vertical_pad):
        result.append("".join(["|", " " * (width - 2), "|"]))

    def draw_cross(cross_count: int) -> None:
        space_count = cross_count - 1
        for _ in range(int(height / 5)):
            result.append("".join([
                "|", " " * space_count, "+" * cross_count, " " * space_count,
                "|"
            ]))

    draw_cross(cross_count=int(width / 5))
    draw_cross(cross_count=3 * int(width / 5))
    draw_cross(cross_count=int(width / 5))

    for _ in range(vertical_pad):
        result.append("".join(["|", " " * (width - 2), "|"]))

    result.append("-" * width)

    return Lines(result)
Exemple #2
0
def draw(height: int) -> Lines:
    """Draw the pattern of size given as ``height`` and return the text lines."""
    if height == 1:
        return Lines(["1"])

    result = []  # type: List[str]

    middle = int(height / 2)
    for i in range(1, height + 1):
        if i < middle:
            digit_count = 1 + 2 * (i - 1)
        elif i == middle:
            digit_count = height
        else:
            digit_count = 1 + 2 * (height - i)
        pad = int((height - digit_count) / 2)

        line = "".join(["." * pad, str(i) * digit_count, "." * pad])
        result.append(line)

    return Lines(result)
Exemple #3
0
    def test_top(self) -> None:
        text = textwrap.dedent("""\
            111111004  5.0  5.0  6.0
            111111005  3.75 3.0  4.0
            111111006  4.5  2.25 4.0""")

        lines = Lines(text.splitlines())

        gradings = problem_01.parse(lines=lines)
        top_gradings = problem_01.top(gradings=gradings, limit=2)

        self.assertListEqual([gradings[0], gradings[1]], top_gradings)
Exemple #4
0
def draw(width: int) -> Lines:
    """Draw the pattern with the size given as ``width`` and return the text lines."""
    result = []  # type: List[str]

    for i in range(1, int(width / 2) + 1):
        if i % 2 == 1:
            pattern = "**.."
        else:
            pattern = "..**"

        result.append(pattern * int(width / 4))

    return Lines(result)
def draw(width: int) -> Lines:
    """Draw the pattern with size given as ``width`` and return the text lines."""
    result = []  # type: List[str]
    half = int(width / 2)

    # ERROR (mristin, 2021-05-10):
    # I forgot to produce a vertical symmetry here. The order of backslashes and
    # slashes need to be inverted after the half (see the correct solution).
    for i in itertools.chain(range(1, half + 1), range(half, 0, -1)):
        left_pad = " " * (half - i)
        backslashes = "\\" * i
        slashes = "/" * i
        result.append(left_pad + backslashes + slashes)

    return Lines(result)
Exemple #6
0
    def test_critical(self) -> None:
        text = textwrap.dedent("""\
            111111004  5.0  5.0  6.0
            111111005  3.75 3.0  4.0
            111111006  4.5  2.25 4.0""")

        lines = Lines(text.splitlines())

        gradings = problem_01.parse(lines=lines)
        critical_gradings = problem_01.critical(
            gradings=gradings,
            bound1=problem_01.Grade(Decimal("4.0")),
            bound2=Decimal("8.0"),
        )

        self.assertListEqual([gradings[1]], critical_gradings)
Exemple #7
0
def draw(width: int) -> Lines:
    """Draw the pattern with size given as ``width`` and return the text lines."""
    result = []  # type: List[str]
    half = int(width / 2)
    for i in range(1, half + 1):
        left_pad = " " * (half - i)
        backslashes = "\\" * i
        slashes = "/" * i
        result.append(left_pad + backslashes + slashes)

    for i in range(half, 0, -1):
        left_pad = " " * (half - i)
        slashes = "/" * i
        backslashes = "\\" * i
        result.append(left_pad + slashes + backslashes)

    return Lines(result)
Exemple #8
0
def parse(lines: Lines) -> List[Block]:
    """Parse the input data given as text ``lines`` into structured blocks."""
    list_of_block_lines = []  # type: List[Lines]

    accumulator = []  # type: List[str]
    for line in lines:
        if line == ".":
            if accumulator:
                list_of_block_lines.append(Lines(accumulator))
                accumulator = []
        else:
            accumulator.append(line)

    if accumulator:
        raise ValueError("Unexpected unfinished block: {}".format(
            "\n".join(accumulator)))

    return [
        parse_block(lines=block_lines) for block_lines in list_of_block_lines
    ]
    def test_case(self) -> None:
        text = textwrap.dedent("""\
            Michaela Meier
            LX326 05.12.2016 ECONOMY
            LX317 10.01.2017 ECONOMY
            A3851 12.05.2017 BUSINESS
            LX8 12.10.2017 FIRST 4433
            .
            Stefan Oliver Schmid
            LX4150 19.10.2017 BUSINESS 6404
            .""")

        lines = Lines(text.splitlines())
        blocks = problem_03.parse(lines=lines)
        totals = problem_03.compute_totals(blocks=blocks)

        self.assertDictEqual(
            {
                "Michaela Meier": 13799,
                "Stefan Oliver Schmid": 12808
            },
            totals,  # type: ignore
        )
def main() -> None:
    """Execute the main routine."""
    min_time, bus_ids = parse_input(Lines(sys.stdin.readlines()))
    departure_time, bus_id = find_departure(min_time, bus_ids)
    wait_time = departure_time - min_time
    print(wait_time * bus_id)
Exemple #11
0
def main() -> None:
    """Execute the main routine."""
    lines = Lines(sys.stdin.read().splitlines())
    print(count_containers(lines))
    def test_case(self) -> None:
        lines = Lines(
            textwrap.dedent(
                """\
            .#.
            ..#
            ###"""
            ).splitlines()
        )

        activity = day_17_conway_cubes.parse_initial(lines=lines)

        expected_reprs = [
            textwrap.dedent(
                """\
                z=0
                .#.
                ..#
                ###"""
            ),
            textwrap.dedent(
                """\
                z=-1
                #..
                ..#
                .#.
                
                z=0
                #.#
                .##
                .#.
                
                z=1
                #..
                ..#
                .#."""
            ),
            textwrap.dedent(
                """\
                z=-2
                .....
                .....
                ..#..
                .....
                .....
                
                z=-1
                ..#..
                .#..#
                ....#
                .#...
                .....
                
                z=0
                ##...
                ##...
                #....
                ....#
                .###.
                
                z=1
                ..#..
                .#..#
                ....#
                .#...
                .....
                
                z=2
                .....
                .....
                ..#..
                .....
                ....."""
            ),
            textwrap.dedent(
                """\
            z=-2
            .......
            .......
            ..##...
            ..###..
            .......
            .......
            .......
            
            z=-1
            ..#....
            ...#...
            #......
            .....##
            .#...#.
            ..#.#..
            ...#...
            
            z=0
            ...#...
            .......
            #......
            .......
            .....##
            .##.#..
            ...#...
            
            z=1
            ..#....
            ...#...
            #......
            .....##
            .#...#.
            ..#.#..
            ...#...
            
            z=2
            .......
            .......
            ..##...
            ..###..
            .......
            .......
            ......."""
            ),
        ]

        self.assertEqual(
            expected_reprs[0],
            day_17_conway_cubes.repr_activity(activity=activity),
        )

        cycle = 1
        for expected_repr in expected_reprs[1:]:
            activity = day_17_conway_cubes.apply(activity=activity)

            self.assertEqual(
                expected_repr,
                day_17_conway_cubes.repr_activity(activity=activity),
                f"after {cycle} cycle",
            )

            cycle += 1

        # Simulate for 3 more cycles
        for _ in range(3):
            activity = day_17_conway_cubes.apply(activity=activity)

        self.assertEqual(112, day_17_conway_cubes.count_active(activity=activity))
def parse_input(puzzle_input: str) -> Lines:
    """Split the puzzle input along the newlines."""
    return Lines(puzzle_input.splitlines())