Created
May 16, 2026 13:27
-
-
Save M0r13n/310fbfe99f98c2bf1639d182368f7329 to your computer and use it in GitHub Desktop.
[WIP] Solver for https://ardoedo.it/kempelen/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import itertools | |
| UNIVERSE = 0xffffffffffffffff | |
| H_LINE = 0x0101010101010101 | |
| A_LINE = 0x0101010101010101 << 7 | |
| def pprint(board: int) -> None: | |
| mask = 0xff00000000000000 | |
| for i in range(7, -1, -1): | |
| print(' '.join(f'{(board & mask) >> (i * 8):08b}')) | |
| mask >>= 8 | |
| def queen_moves(pos, board) -> int: | |
| moves = pos | |
| sq = pos | |
| for _ in range(7): | |
| # shift right | |
| sq = (sq >> 1) & ~A_LINE | |
| if sq & board: break | |
| moves |= sq | |
| sq = pos | |
| for _ in range(7): | |
| # shift left | |
| sq = ((sq << 1) & ~H_LINE) | |
| if sq & board: break | |
| moves |= sq | |
| sq = pos | |
| for _ in range(7): | |
| # shift up | |
| sq = (sq << 8) | |
| if sq & board: break | |
| moves |= sq | |
| sq = pos | |
| for _ in range(7): | |
| # shift down | |
| sq = (sq >> 8) | |
| if sq & board: break | |
| moves |= sq | |
| sq = pos | |
| for _ in range(7): | |
| # shift right up | |
| sq = (((sq>>1) << 8) & ~A_LINE) | |
| if sq & board: break | |
| moves |= sq | |
| sq = pos | |
| for _ in range(7): | |
| # shift left up | |
| sq = (((sq<<1) << 8) & ~H_LINE) | |
| if sq & board: break | |
| moves |= sq | |
| sq = pos | |
| for _ in range(7): | |
| # shift right down | |
| sq = (((sq>>1) >> 8) & ~A_LINE) | |
| if sq & board: break | |
| moves |= sq | |
| sq = pos | |
| for _ in range(7): | |
| # shift left down | |
| sq = (((sq<<1) >> 8) & ~H_LINE) | |
| if sq & board: break | |
| moves |= sq | |
| return moves | |
| def bishop_moves(pos, board) -> int: | |
| moves = pos | |
| sq = pos | |
| for _ in range(7): | |
| # shift right up | |
| sq = (((sq>>1) << 8) & ~A_LINE) | |
| if sq & board: break | |
| moves |= sq | |
| sq = pos | |
| for _ in range(7): | |
| # shift left up | |
| sq = (((sq<<1) << 8) & ~H_LINE) | |
| if sq & board: break | |
| moves |= sq | |
| sq = pos | |
| for _ in range(7): | |
| # shift right down | |
| sq = (((sq>>1) >> 8) & ~A_LINE) | |
| if sq & board: break | |
| moves |= sq | |
| sq = pos | |
| for _ in range(7): | |
| # shift left down | |
| sq = (((sq<<1) >> 8) & ~H_LINE) | |
| if sq & board: break | |
| moves |= sq | |
| return moves | |
| if __name__ == '__main__': | |
| for pieces in itertools.product(range(64), repeat=5): | |
| board = (128 >> (pieces[0] % 8)) << ((pieces[0] // 8) * 8) | |
| board |= (128 >> (pieces[1] % 8)) << ((pieces[1] // 8) * 8) | |
| board |= (128 >> (pieces[2] % 8)) << ((pieces[2] // 8) * 8) | |
| board |= (128 >> (pieces[3] % 8)) << ((pieces[3] // 8) * 8) | |
| board |= (128 >> (pieces[4] % 8)) << ((pieces[4] // 8) * 8) | |
| attacked = queen_moves((128 >> (pieces[0] % 8)) << ((pieces[0] // 8) * 8), board) | |
| attacked |= queen_moves((128 >> (pieces[1] % 8)) << ((pieces[1] // 8) * 8), board) | |
| attacked |= queen_moves((128 >> (pieces[2] % 8)) << ((pieces[2] // 8) * 8), board) | |
| attacked |= queen_moves((128 >> (pieces[3] % 8)) << ((pieces[3] // 8) * 8), board) | |
| attacked |= bishop_moves((128 >> (pieces[4] % 8)) << ((pieces[4] // 8) * 8), board) | |
| # if len(set(pieces)) > 4: | |
| # print("d") | |
| # pprint(board) | |
| # print() | |
| # pprint((128 >> (pieces[0] % 8)) << ((pieces[0] // 8) * 8)) | |
| # print() | |
| # pprint(attacked) | |
| # break | |
| if (attacked & UNIVERSE) == UNIVERSE: | |
| board = (128 >> (pieces[0] % 8)) << ((pieces[0] // 8) * 8) | |
| board |= (128 >> (pieces[1] % 8)) << ((pieces[1] // 8) * 8) | |
| board |= (128 >> (pieces[2] % 8)) << ((pieces[2] // 8) * 8) | |
| board |= (128 >> (pieces[3] % 8)) << ((pieces[3] // 8) * 8) | |
| #board |= (128 >> (pieces[4] % 8)) << ((pieces[4] // 8) * 8) | |
| pprint(board) | |
| break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment