def test_image_and_solve(): file_path = FILE_PATH.parent / "img/lvl_725.jpg" with open(file_path, 'rb') as f: file_bytes = np.asarray(bytearray(f.read()), dtype=np.uint8) image_parser = ImageParser(file_bytes) colors = image_parser.to_colors() puzzle = BallSortPuzzle(colors) result = puzzle.solve() assert result is True assert ( str(puzzle.moves) == "[Ball(0 -> 12), Ball(1 -> 13), Ball(7 -> 12), Ball(11 -> 1), Ball(13 -> 11), Ball(3 -> 13), " "Ball(4 -> 13), Ball(9 -> 4), Ball(3 -> 9), Ball(12 -> 3), Ball(12 -> 3), Ball(4 -> 12), " "Ball(4 -> 12), Ball(0 -> 4), Ball(6 -> 0), Ball(11 -> 12), Ball(11 -> 12), Ball(7 -> 11), " "Ball(4 -> 7), Ball(4 -> 7), Ball(4 -> 11), Ball(1 -> 4), Ball(1 -> 4), Ball(5 -> 4), Ball(2 -> " "5), Ball(8 -> 4), Ball(5 -> 2), Ball(9 -> 8), Ball(2 -> 5), Ball(10 -> 9), Ball(1 -> 10), " "Ball(5 -> 2), Ball(10 -> 1), Ball(10 -> 1), Ball(2 -> 5), Ball(10 -> 6), Ball(5 -> 2), " "Ball(10 -> 13), Ball(2 -> 5), Ball(3 -> 10), Ball(3 -> 10), Ball(3 -> 10), Ball(2 -> 3), " "Ball(2 -> 10), Ball(5 -> 3), Ball(5 -> 3), Ball(2 -> 5), Ball(6 -> 2), Ball(6 -> 2), Ball(5 -> " "6), Ball(5 -> 6), Ball(5 -> 1), Ball(2 -> 5), Ball(2 -> 5), Ball(6 -> 2), Ball(6 -> 2), " "Ball(6 -> 2), Ball(6 -> 13), Ball(5 -> 6), Ball(5 -> 6), Ball(7 -> 5), Ball(7 -> 5), Ball(7 -> " "5), Ball(7 -> 2), Ball(6 -> 7), Ball(6 -> 7), Ball(8 -> 6), Ball(8 -> 6), Ball(7 -> 8), " "Ball(7 -> 8), Ball(6 -> 7), Ball(6 -> 7), Ball(8 -> 6), Ball(8 -> 6), Ball(8 -> 6), Ball(8 -> " "5), Ball(7 -> 8), Ball(7 -> 8), Ball(9 -> 7), Ball(7 -> 8), Ball(9 -> 7), Ball(7 -> 8), " "Ball(9 -> 7), Ball(9 -> 0), Ball(7 -> 9), Ball(11 -> 7), Ball(7 -> 9), Ball(11 -> 7), " "Ball(7 -> 9), Ball(11 -> 7), Ball(7 -> 9), Ball(11 -> 6)]" )
def handler(event: Optional[dict], context: Optional[dict]): body = json.loads(event['body']) # type: ignore print(body) message = body['message'] chat_id = message['chat']['id'] if photos := message.get('photo'): # here photos is an array with same photo of different sizes hd_photo = max(photos, key=lambda x: x['file_size'] ) # get one with the highest resolution try: file = telegram_client.download_file(hd_photo['file_id']) except TelegramClientError: text = "Cant download the image from TG :(" else: file_bytes = np.asarray(bytearray(file.read()), dtype=np.uint8) try: image_parser = ImageParser(file_bytes) colors = image_parser.to_colors() except ImageParserError as exp: text = f"Cant parse image: {exp}" else: puzzle = BallSortPuzzle(colors) solved = puzzle.solve() if solved: text = get_telegram_repr(puzzle) else: text = "This lvl don't have a solution"
def test_3x3(): data_in = [ [color.RED, color.GREEN, color.RED], [color.GREEN, color.RED, color.GREEN], [], ] puzzle = BallSortPuzzle(data_in) result = puzzle.solve() assert result is True play_moves(data_in, puzzle.moves)
def test_4x3(): data_in = [ [color.BLUE, color.ORANGE, color.BLUE, color.ORANGE], [color.ORANGE, color.BLUE, color.ORANGE, color.BLUE], [], ] puzzle = BallSortPuzzle(data_in) result = puzzle.solve() assert result is True play_moves(data_in, puzzle.moves)
def test_no_solution(filename): file_path = FILE_PATH.parent / filename with open(file_path, 'rb') as f: file_bytes = np.asarray(bytearray(f.read()), dtype=np.uint8) image_parser = ImageParser(file_bytes, debug=False) colors = image_parser.to_colors() assert colors puzzle = BallSortPuzzle(colors) result = puzzle.solve() assert result is False
def test_image3(): file_path = FILE_PATH.parent / "img/lvl_1051.jpg" with open(file_path, 'rb') as f: file_bytes = np.asarray(bytearray(f.read()), dtype=np.uint8) image_parser = ImageParser(file_bytes) colors = image_parser.to_colors() assert colors puzzle = BallSortPuzzle(colors) result = puzzle.solve() assert result is True
def test_4x7(): data_in = [ [color.L_GREEN, color.ORANGE, color.BLUE, color.PINK], [color.ORANGE, color.L_GREEN, color.BLUE, color.PINK], [color.PINK, color.RED, color.ORANGE, color.RED], [color.ORANGE, color.PINK, color.RED, color.BLUE], [color.L_GREEN, color.L_GREEN, color.RED, color.BLUE], [], [], ] puzzle = BallSortPuzzle(data_in) result = puzzle.solve() assert result is True play_moves(data_in, puzzle.moves)
def test_4x14(): data_in = [ [color.L_BLUE, color.L_BLUE, color.GREEN, color.L_GREEN], [color.PINK, color.PINK, color.BLUE, color.BROWN], [color.LIME, color.L_GREEN, color.VIOLET, color.VIOLET], [color.VIOLET, color.L_GREEN, color.GRAY, color.ORANGE], [color.RED, color.GREEN, color.BROWN, color.ORANGE], [color.PINK, color.LIME, color.VIOLET, color.BLUE], [color.ORANGE, color.LIME, color.YELLOW, color.L_BLUE], [color.LIME, color.GREEN, color.RED, color.L_GREEN], [color.GREEN, color.YELLOW, color.GRAY, color.BLUE], [color.L_BLUE, color.RED, color.GRAY, color.BROWN], [color.ORANGE, color.YELLOW, color.PINK, color.GRAY], [color.YELLOW, color.RED, color.BROWN, color.BLUE], [], [], ] puzzle = BallSortPuzzle(data_in) result = puzzle.solve() assert result is True play_moves(data_in, puzzle.moves)