Fragment validation and adjacents pieces

master
km0 3 years ago
parent 89b15ac3b6
commit bbcdad5328

@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 22, "execution_count": 27,
"id": "7b187c4c-1c53-494d-a30f-fa0431cd4665", "id": "7b187c4c-1c53-494d-a30f-fa0431cd4665",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -10,7 +10,8 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Done!\n" "Done!\n",
"[['LdPQ', 'TqTD', 'oXxV', 'SDED', 'f4fi', 'AFuQ', '6aQ6', 'MRkN', '5Yih', 'neh7', '4iFu', 'hcuu', 'MKvC', 'FQah', 'XBV3'], ['kJ2u', 'LhB2', '663j', 'Nge2', 'gJAQ', 'i3fZ', 'DxYS', 'Yu4x', 'SEQx', '26qm', 'EBEq', 'iNFA', 'Usde', '3vWt', 'G9E2'], ['WQ2k', 'R3mh', 'C9x4', 'Nnrz', 'mvF7', '2qUx', 'YVnK', '2SKA', 'mEeF', 'FSKH', 'UNsr', 'FC6x', 'kDbf', 'dGe9', 'kZWC'], ['55Sn', 'gMaU', 'NUXW', 'JkmV', '9zgi', 'UZoN', 'bsvK', 'U67P', '2RLV', 'ktxF', 'Eh3T', 'jExX', 'ZKW9', 'NRAT', '6J8b'], ['ct9T', 'KNwy', 'EvPM', 'is6X', 'Bw4u', 'j7Mc', '9wEb', 'Gbog', 'Rq4G', 'Li4Z', 'e5VP', 'YByC', '5us6', '7HqJ', 'hgzi'], ['F6Jp', 'Sb66', 'F2KR', 'KT4E', 'SsDP', 'jRvw', 'WZGh', 'XmH3', 'YLts', 'XPW5', 'WwQr', '9qHo', 'gYWg', 'XZwu', 'YQNb'], ['GCLu', 'd7q6', 'NgZc', 'jahF', '7826', 'nijr', '5eZR', '4ajp', 'NVT7', 'LU3x', 'fRTu', 'HEbf', 'Ykek', 'm9oE', 'KB8h'], ['Z9a6', 'QEBi', '7Bo6', 'bmBt', 'LcaY', 'RmVm', 'FrPH', 'NYnu', 'kv9R', 'A4CU', 'K6CG', 'NNF8', 'BfFx', 'ktn4', 'RBKH'], ['Zq9j', 'aBXk', 'VJh7', 'iqby', 'Qasx', 'Lttd', 'RVkq', 'kEqx', '6k5s', 'nqEX', '6gXY', 'Fvg6', 'H2FQ', '2TAE', 'CCGn'], ['cUGD', 'GqAj', 'P79K', 'MCZm', 'bddT', '6ZZD', 'XeBV', 'jdZG', 'g8Bp', 'f2yf', '5eHV', 'XZCU', 'UDPC', 'RWXt', 'FAFT']]\n"
] ]
} }
], ],
@ -46,29 +47,160 @@
"piece_height = height / columns\n", "piece_height = height / columns\n",
"\n", "\n",
"\n", "\n",
"pieces = {}\n", "pieces = []\n",
"adjacents = {}\n",
"\n",
"\n", "\n",
"with Image(width = width, height = height) as img:\n", "with Image(width = width, height = height) as img:\n",
" draw = Drawing()\n", " draw = Drawing()\n",
" draw.font_size = 9\n", " draw.font_size = 11\n",
" draw.text_alignment = 'center'\n", " draw.text_alignment = 'center'\n",
" \n", " \n",
" for y in range(columns):\n", " for x in range(rows):\n",
" for x in range(rows):\n", " row = []\n",
" \n", " for y in range(columns):\n",
"\n",
" # generate a random ID for each piece \n", " # generate a random ID for each piece \n",
" ID = uuid()[:4]\n", " ID = uuid()[:4]\n",
" \n", " \n",
" # write the id in the center of each piece\n", " # write the id in the center of each piece\n",
" draw.text(int(x * piece_width + piece_width/2), int(y*piece_height + piece_height/2), f'{name}\\n{ID}')\n", " draw.text(int(x * piece_width + piece_width/2), int(y*piece_height + piece_height/2), f'{name}\\n{ID}')\n",
" \n", " \n",
" # store the id and the position in the pieces dictionary\n", " # store the id in the pieces dictionary\n",
" pieces[ID] = (x,y)\n", " row.append(ID)\n",
" \n",
" pieces.append(row)\n",
" \n", " \n",
" draw(img)\n", " draw(img)\n",
" img.save(filename=f'{name}_retro.png')\n", " img.save(filename=f'{name}_retro.png')\n",
" print('Done!')\n" " print('Done!')"
] ]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "ea7900fd-a448-4101-87d9-214a60283579",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"TqTD\n"
]
}
],
"source": [
"print(pieces[0][1])"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "b77b827b-4bc1-4f20-b33e-7c8f6d0118c7",
"metadata": {},
"outputs": [],
"source": [
"for x in range(rows):\n",
" for y in range(columns):\n",
" \n",
" current = pieces[x][y]\n",
" \n",
" if y > 0:\n",
" n = pieces[x][y-1]\n",
" else:\n",
" n = None\n",
" \n",
" if y < columns - 1:\n",
" s = pieces[x][y+1]\n",
" else:\n",
" s = None\n",
" \n",
" if x > 0:\n",
" w = pieces[x-1][y]\n",
" else: \n",
" w = None\n",
" \n",
" if x < rows - 1:\n",
" e = pieces[x+1][y]\n",
" else:\n",
" e = None\n",
" \n",
" adjacents[current] = (n, e, s, w)\n"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "2a93542f-78fa-4ba4-b83b-8036165e463a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(None, 'kJ2u', 'TqTD', None)\n"
]
}
],
"source": [
"print(adjacents['LdPQ'])"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "aff104cd-e2ff-4471-968d-93b22f0481df",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The fragment is valid!\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fragment = ['f4fi', 'AFuQ', 'i3fZ', '2qUx', 'mvF7',]\n",
"\n",
"def is_valid_fragment(pieces):\n",
" valid = True\n",
" for p0 in pieces:\n",
" connection = []\n",
" for p1 in pieces:\n",
" if p0 == p1:\n",
" continue\n",
" connected = p0 in adjacents[p1]\n",
" connection.append(connected)\n",
" if not True in connection:\n",
" valid = False\n",
" print(f'Not valid fragment, piece {p0} is not connected')\n",
" break\n",
" print('The fragment is valid!')\n",
" return valid\n",
" \n",
" \n",
"is_valid_fragment(fragment)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9e0cafe7-ce47-4a24-a41f-d7949f632225",
"metadata": {},
"outputs": [],
"source": []
} }
], ],
"metadata": { "metadata": {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

After

Width:  |  Height:  |  Size: 40 KiB

@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 22, "execution_count": 27,
"id": "7b187c4c-1c53-494d-a30f-fa0431cd4665", "id": "7b187c4c-1c53-494d-a30f-fa0431cd4665",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -10,7 +10,8 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Done!\n" "Done!\n",
"[['LdPQ', 'TqTD', 'oXxV', 'SDED', 'f4fi', 'AFuQ', '6aQ6', 'MRkN', '5Yih', 'neh7', '4iFu', 'hcuu', 'MKvC', 'FQah', 'XBV3'], ['kJ2u', 'LhB2', '663j', 'Nge2', 'gJAQ', 'i3fZ', 'DxYS', 'Yu4x', 'SEQx', '26qm', 'EBEq', 'iNFA', 'Usde', '3vWt', 'G9E2'], ['WQ2k', 'R3mh', 'C9x4', 'Nnrz', 'mvF7', '2qUx', 'YVnK', '2SKA', 'mEeF', 'FSKH', 'UNsr', 'FC6x', 'kDbf', 'dGe9', 'kZWC'], ['55Sn', 'gMaU', 'NUXW', 'JkmV', '9zgi', 'UZoN', 'bsvK', 'U67P', '2RLV', 'ktxF', 'Eh3T', 'jExX', 'ZKW9', 'NRAT', '6J8b'], ['ct9T', 'KNwy', 'EvPM', 'is6X', 'Bw4u', 'j7Mc', '9wEb', 'Gbog', 'Rq4G', 'Li4Z', 'e5VP', 'YByC', '5us6', '7HqJ', 'hgzi'], ['F6Jp', 'Sb66', 'F2KR', 'KT4E', 'SsDP', 'jRvw', 'WZGh', 'XmH3', 'YLts', 'XPW5', 'WwQr', '9qHo', 'gYWg', 'XZwu', 'YQNb'], ['GCLu', 'd7q6', 'NgZc', 'jahF', '7826', 'nijr', '5eZR', '4ajp', 'NVT7', 'LU3x', 'fRTu', 'HEbf', 'Ykek', 'm9oE', 'KB8h'], ['Z9a6', 'QEBi', '7Bo6', 'bmBt', 'LcaY', 'RmVm', 'FrPH', 'NYnu', 'kv9R', 'A4CU', 'K6CG', 'NNF8', 'BfFx', 'ktn4', 'RBKH'], ['Zq9j', 'aBXk', 'VJh7', 'iqby', 'Qasx', 'Lttd', 'RVkq', 'kEqx', '6k5s', 'nqEX', '6gXY', 'Fvg6', 'H2FQ', '2TAE', 'CCGn'], ['cUGD', 'GqAj', 'P79K', 'MCZm', 'bddT', '6ZZD', 'XeBV', 'jdZG', 'g8Bp', 'f2yf', '5eHV', 'XZCU', 'UDPC', 'RWXt', 'FAFT']]\n"
] ]
} }
], ],
@ -46,29 +47,160 @@
"piece_height = height / columns\n", "piece_height = height / columns\n",
"\n", "\n",
"\n", "\n",
"pieces = {}\n", "pieces = []\n",
"adjacents = {}\n",
"\n",
"\n", "\n",
"with Image(width = width, height = height) as img:\n", "with Image(width = width, height = height) as img:\n",
" draw = Drawing()\n", " draw = Drawing()\n",
" draw.font_size = 9\n", " draw.font_size = 11\n",
" draw.text_alignment = 'center'\n", " draw.text_alignment = 'center'\n",
" \n", " \n",
" for y in range(columns):\n", " for x in range(rows):\n",
" for x in range(rows):\n", " row = []\n",
" \n", " for y in range(columns):\n",
"\n",
" # generate a random ID for each piece \n", " # generate a random ID for each piece \n",
" ID = uuid()[:4]\n", " ID = uuid()[:4]\n",
" \n", " \n",
" # write the id in the center of each piece\n", " # write the id in the center of each piece\n",
" draw.text(int(x * piece_width + piece_width/2), int(y*piece_height + piece_height/2), f'{name}\\n{ID}')\n", " draw.text(int(x * piece_width + piece_width/2), int(y*piece_height + piece_height/2), f'{name}\\n{ID}')\n",
" \n", " \n",
" # store the id and the position in the pieces dictionary\n", " # store the id in the pieces dictionary\n",
" pieces[ID] = (x,y)\n", " row.append(ID)\n",
" \n",
" pieces.append(row)\n",
" \n", " \n",
" draw(img)\n", " draw(img)\n",
" img.save(filename=f'{name}_retro.png')\n", " img.save(filename=f'{name}_retro.png')\n",
" print('Done!')\n" " print('Done!')"
] ]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "ea7900fd-a448-4101-87d9-214a60283579",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"TqTD\n"
]
}
],
"source": [
"print(pieces[0][1])"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "b77b827b-4bc1-4f20-b33e-7c8f6d0118c7",
"metadata": {},
"outputs": [],
"source": [
"for x in range(rows):\n",
" for y in range(columns):\n",
" \n",
" current = pieces[x][y]\n",
" \n",
" if y > 0:\n",
" n = pieces[x][y-1]\n",
" else:\n",
" n = None\n",
" \n",
" if y < columns - 1:\n",
" s = pieces[x][y+1]\n",
" else:\n",
" s = None\n",
" \n",
" if x > 0:\n",
" w = pieces[x-1][y]\n",
" else: \n",
" w = None\n",
" \n",
" if x < rows - 1:\n",
" e = pieces[x+1][y]\n",
" else:\n",
" e = None\n",
" \n",
" adjacents[current] = (n, e, s, w)\n"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "2a93542f-78fa-4ba4-b83b-8036165e463a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(None, 'kJ2u', 'TqTD', None)\n"
]
}
],
"source": [
"print(adjacents['LdPQ'])"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "aff104cd-e2ff-4471-968d-93b22f0481df",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The fragment is valid!\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fragment = ['f4fi', 'AFuQ', 'i3fZ', '2qUx', 'mvF7',]\n",
"\n",
"def is_valid_fragment(pieces):\n",
" valid = True\n",
" for p0 in pieces:\n",
" connection = []\n",
" for p1 in pieces:\n",
" if p0 == p1:\n",
" continue\n",
" connected = p0 in adjacents[p1]\n",
" connection.append(connected)\n",
" if not True in connection:\n",
" valid = False\n",
" print(f'Not valid fragment, piece {p0} is not connected')\n",
" break\n",
" print('The fragment is valid!')\n",
" return valid\n",
" \n",
" \n",
"is_valid_fragment(fragment)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9e0cafe7-ce47-4a24-a41f-d7949f632225",
"metadata": {},
"outputs": [],
"source": []
} }
], ],
"metadata": { "metadata": {

Loading…
Cancel
Save