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
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"]], }
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
def test_calc_neighbor_map_simple(): seats = solution.parse([ ".............", ".L.L.#.#.#.#.", ".............", ]) nmap = solution.calc_neighbor_map(seats) assert nmap[(1, 1)] == {(3, 1)}
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")
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), }
def test_part_1(file: Path, expected): lines = file.read_text().splitlines() result = solution.part_1(solution.parse(lines)) assert result == expected
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()
def test_reduce_step(number, expected): number = parse(number) assert reduce_step(number) assert number == parse(expected)
def test_magnitude(number, expected): parsed = parse(number) assert unflatten(parse(number)) == number assert magnitude(parsed) == expected
def test_parse_no_precedence(expression, result): assert " ".join(parse(expression, operator_precendence={ "+": 0, "*": 0 })) == result
def test_parse(): assert list(parse([[[[[9,8],1],2],3],4])) == [(9, 4), (8, 4), (1, 3), (2, 2), (3, 1), (4, 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)
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")
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")
def test_evaluation_mathematical_precendence(expression): assert evaluate(parse(expression, operator_precendence={ "*": 1, "+": 0 })) == eval(expression)
def test_should_make_deadfish_swim(data, expected): assert parse(data) == expected
def test_evaluate_addition_precedence(expression, result): assert evaluate(parse(expression, operator_precendence={ "+": 1, "*": 0 })) == result