Created
December 26, 2023 12:18
-
-
Save nmueas/308c7d91f8ad1ed0c86cdf9ed5522976 to your computer and use it in GitHub Desktop.
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
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 19, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import re\n", | |
| "from collections import defaultdict\n", | |
| "\n", | |
| "# https://github.com/CGLemon/chinese-chess-PGN\n", | |
| "with open(\"WXF-41743games.pgns\", encoding=\"utf8\") as f:\n", | |
| " data = f.read()\n", | |
| "\n", | |
| "cc = defaultdict(lambda:defaultdict(int))\n", | |
| "\n", | |
| "for d in data.split(\"\\n\\n\"):\n", | |
| " bd = [[None for j in range(10)] for i in range(9)]\n", | |
| " for i, p in enumerate([(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0), (8, 0), (1, 2), (7, 2), (0, 3), (2, 3), (4, 3), (6, 3), (8, 3)]):\n", | |
| " bd[p[0]][p[1]] = i\n", | |
| " last_m = (-1, 0, 0)\n", | |
| " for z in re.findall(r\"\\d+\\. (..)-(..) (..)-(..)\", d):\n", | |
| " p1, p2, q1, q2 = tuple(map(lambda t: (ord(t[0]) - ord(\"A\"), int(t[1])), z))\n", | |
| " assert bd[p1[0]][p1[1]] != None\n", | |
| " c = bd[p1[0]][p1[1]]\n", | |
| " m = (c, (p2[0] - p1[0] + 9) % 9, (p2[1] - p1[1] + 10) % 10)\n", | |
| " bd[p2[0]][p2[1]] = bd[p1[0]][p1[1]]\n", | |
| " bd[p1[0]][p1[1]] = None\n", | |
| " cc[last_m][m] += 1\n", | |
| " last_m = m\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 21, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "6.019194684029047\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "\n", | |
| "ts = 0\n", | |
| "tm = 0\n", | |
| "for m, cnt in cc.items():\n", | |
| " freq = set((cnt[k], k) for k in cnt)\n", | |
| " total_moves = sum(cnt.values())\n", | |
| " total_bits = 0\n", | |
| " while len(freq) > 1:\n", | |
| " x = min(freq, key=lambda t: t[0])\n", | |
| " freq.remove(x)\n", | |
| " y = min(freq, key=lambda t: t[0])\n", | |
| " freq.remove(y)\n", | |
| " freq.add((x[0] + y[0], (x[1], y[1])))\n", | |
| " total_bits += x[0] + y[0]\n", | |
| " ts += total_bits\n", | |
| " tm += total_moves\n", | |
| "print(ts / tm)\n" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.11.2" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment