Beispiel #1
0
def test_add_large_example_steps():
    numbers = """
        [[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]]
        [7,[[[3,7],[4,3]],[[6,3],[8,8]]]]
        [[2,[[0,8],[3,4]]],[[[6,7],1],[7,[1,6]]]]
        [[[[2,4],7],[6,[0,5]]],[[[6,8],[2,8]],[[2,1],[4,5]]]]
        [7,[5,[[3,8],[1,4]]]]
        [[2,[2,2]],[8,[8,1]]]
        [2,9]
        [1,[[[9,3],9],[[9,0],[0,7]]]]
        [[[5,[7,4]],7],1]
        [[[[4,2],2],6],[8,7]]
    """
    numbers = [parse(json.loads(line)) for line in numbers.strip().splitlines()]
    expected = [
        [[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]],
        [[[[4,0],[5,4]],[[7,7],[6,0]]],[[8,[7,7]],[[7,9],[5,0]]]],
        [[[[6,7],[6,7]],[[7,7],[0,7]]],[[[8,7],[7,7]],[[8,8],[8,0]]]],
        [[[[7,0],[7,7]],[[7,7],[7,8]]],[[[7,7],[8,8]],[[7,7],[8,7]]]],
        [[[[7,7],[7,8]],[[9,5],[8,7]]],[[[6,8],[0,8]],[[9,9],[9,0]]]],
        [[[[6,6],[6,6]],[[6,0],[6,7]]],[[[7,7],[8,9]],[8,[8,1]]]],
        [[[[6,6],[7,7]],[[0,7],[7,7]]],[[[5,5],[5,6]],9]],
        [[[[7,8],[6,7]],[[6,8],[0,8]]],[[[7,7],[5,0]],[[5,5],[5,6]]]],
        [[[[7,7],[7,7]],[[8,7],[8,7]]],[[[7,0],[7,7]],9]],
        [[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]],
    ]
    expected = [parse(number) for number in expected]
    for i, (res, exp) in enumerate(zip(accumulate(numbers, add), expected)):
        assert res == exp
Beispiel #2
0
def test_parse():
    assert solution.parse(["0: 1 2", "1: a", "2: b", "3: 1 2 | 2 1"])[0] == {
        "0": [["1", "2"]],
        "1": "a",
        "2": "b",
        "3": [["1", "2"], ["2", "1"]],
    }
Beispiel #3
0
def test_part_2(file: Path, expected):
    if expected is None:
        return
    lines = file.read_text().splitlines()

    result = solution.part_2(solution.parse(lines))
    assert result == expected
Beispiel #4
0
def test_calc_neighbor_map_simple():
    seats = solution.parse([
        ".............",
        ".L.L.#.#.#.#.",
        ".............",
    ])
    nmap = solution.calc_neighbor_map(seats)

    assert nmap[(1, 1)] == {(3, 1)}
Beispiel #5
0
def test_part2_in_detail():
    print()
    lines = Path("test_input2.txt").read_text().splitlines()
    rules, messages = solution.parse(lines)

    rules["8"] = [["42"], ["42", "8"]]
    rules["11"] = [["42", "31"], ["500", "31"]]
    rules["500"] = [["42", "11"]]

    assert solution.cyk(rules, "babbbbaabbbbbabbbbbbaabaaabaaa")
Beispiel #6
0
def test_calc_neighbor_map_2():
    seats = solution.parse([
        ".......#.",
        "...#.....",
        ".#.......",
        ".........",
        "..#L....#",
        "....#....",
        ".........",
        "#........",
        "...#.....",
    ])
    nmap = solution.calc_neighbor_map(seats)

    assert nmap[(3, 4)] == {
        (7, 0),
        (3, 1),
        (1, 2),
        (2, 4),
        (8, 4),
        (4, 5),
        (0, 7),
        (3, 8),
    }
Beispiel #7
0
def test_part_1(file: Path, expected):
    lines = file.read_text().splitlines()

    result = solution.part_1(solution.parse(lines))
    assert result == expected
Beispiel #8
0
            shapes.append(shape)

            # Add border
            print(shape.center_x, shape.center_y)
            rect = create_rectangle_outline(
                shape.center_x,
                shape.center_y,
                len(tile.lines) * PIXEL,
                len(tile.lines) * PIXEL,
                RED,
                # border_width=1,
            )
            shapes.append(rect)

        return shapes

    def on_draw(self):
        arcade.start_render()
        for shape in self.shapes:
            shape.draw()


if __name__ == "__main__":
    lines = Path("test_input.txt").read_text().splitlines()
    tiles = solution.parse(lines)

    win = Window()
    win.show_view(TileView(tiles))

    arcade.run()
Beispiel #9
0
def test_reduce_step(number, expected):
    number = parse(number)
    assert reduce_step(number)
    assert number == parse(expected)
Beispiel #10
0
def test_magnitude(number, expected):
    parsed = parse(number)
    assert unflatten(parse(number)) == number
    assert magnitude(parsed) == expected
Beispiel #11
0
def test_parse_no_precedence(expression, result):
    assert " ".join(parse(expression, operator_precendence={
        "+": 0,
        "*": 0
    })) == result
Beispiel #12
0
def test_parse():
    assert list(parse([[[[[9,8],1],2],3],4])) == [(9, 4), (8, 4), (1, 3), (2, 2), (3, 1), (4, 0)]
Beispiel #13
0
def test_add(numbers, expected):
    for line in numbers.strip().splitlines():
        assert unflatten(parse(json.loads(line))) == json.loads(line)

    numbers = [parse(json.loads(line)) for line in numbers.strip().splitlines()]
    assert fold(add, numbers) == parse(expected)
Beispiel #14
0
def test_youtube_example():
    print()
    rules, _ = solution.parse(
        ["0: 1 2 | 2 3", "1: 2 1 | a", "2: 3 3 | b", "3: 1 2 | a"])

    assert solution.cyk(rules, "abaab")
Beispiel #15
0
def test_complex_paper_example():
    rules, _ = solution.parse(
        ["0: 1 2", "1: 3 4 | 4 3", "2: 3 4 | 4 3", '3: "a"', '4: "b"'])

    assert solution.cyk(rules, "abba")
    assert not solution.cyk(rules, "bbba")
Beispiel #16
0
def test_evaluation_mathematical_precendence(expression):
    assert evaluate(parse(expression, operator_precendence={
        "*": 1,
        "+": 0
    })) == eval(expression)
Beispiel #17
0
def test_should_make_deadfish_swim(data, expected):
    assert parse(data) == expected
Beispiel #18
0
def test_evaluate_addition_precedence(expression, result):
    assert evaluate(parse(expression, operator_precendence={
        "+": 1,
        "*": 0
    })) == result