Skip to content

Instantly share code, notes, and snippets.

@Hultner
Created April 4, 2020 18:21
Show Gist options
  • Save Hultner/d8edb95177df6c179ed7de8504ad95f0 to your computer and use it in GitHub Desktop.
Save Hultner/d8edb95177df6c179ed7de8504ad95f0 to your computer and use it in GitHub Desktop.
Lock puzzle from nordic python
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 226,
"metadata": {},
"outputs": [],
"source": [
"from itertools import combinations, cycle, islice, product\n",
"\n",
"def digit_right_wrong_place(guess, digits):\n",
" return [\n",
" x\n",
" for x, y, z in\n",
" # Combine guess by given digits in the wrong place\n",
" zip(\n",
" guess, \n",
" # Move given digits by offset, end could be inifnite\n",
" islice(cycle(digits), 1, 4),\n",
" islice(cycle(digits), 2, 5)\n",
" )\n",
" if x == y or x == z\n",
" ]\n",
"\n",
"def r2(guess, digits=(2,0,6)):\n",
" matches = digit_right_wrong_place(guess, digits)\n",
" # Convert to set to get unique matches, as it should be two\n",
" # digits and not the same in two places\n",
" return len(matches) == len(set(matches)) == 2\n",
"\n",
"rules = (\n",
" # One digit is right and in it's place\n",
" lambda guess, digits=(6,8,2): \n",
" len([True for x, y in zip(guess, digits) if x == y]) == 1,\n",
" \n",
" # One digit is right but in the wrong place\n",
" lambda guess, digits=(6,1,4): \n",
" len(digit_right_wrong_place(guess, digits)) == 1,\n",
" \n",
" # Two digits is right but both are in the wrong place\n",
" r2,\n",
" \n",
" # All digits are wrong\n",
" lambda guess, digits=(7,3,8):\n",
" any(x in digits for x in guess) == False,\n",
" \n",
" # One digit is right but in the wrong place\n",
" lambda guess, digits=(3,8,0): \n",
" len(digit_right_wrong_place(guess, digits)) == 1,\n",
" \n",
")\n",
"\n",
"def validate(guess):\n",
" return all(rule(guess) for rule in rules)"
]
},
{
"cell_type": "code",
"execution_count": 204,
"metadata": {},
"outputs": [],
"source": [
"def test_rule(rule, correct, incorrect):\n",
" def test_all(guesses, answer): \n",
" assert all(\n",
" rules[rule](x) == answer\n",
" for x in (guesses)\n",
" ) == True\n",
" \n",
" test_all(correct, True)\n",
" test_all(incorrect, False)\n"
]
},
{
"cell_type": "code",
"execution_count": 205,
"metadata": {},
"outputs": [],
"source": [
"# One digit is right and in it's place 682\n",
"test_rule(\n",
" 0, \n",
" ((6,0,0), (1,8,0), (1,1,2)), \n",
" ((6,8,2), (1,2,0), (2,6,8)),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 206,
"metadata": {},
"outputs": [],
"source": [
"# One digit is right but in the wrong place 614\n",
"test_rule(\n",
" 1, \n",
" ((9,6,9), (0,0,6), (1,0,0), (4,0,0)), \n",
" ((0,0,0),(6,1,4), (4,6,1), (1,4,6)),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 207,
"metadata": {},
"outputs": [],
"source": [
"# Two digits is right but both are in the wrong place 206\n",
"test_rule(\n",
" 2, \n",
" ((0,2,1), (0,6,9), (6,9,2), (6,0,0)), \n",
" ((0,0,0),(2,0,6), (6,2,0), (0,2,0)),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 208,
"metadata": {},
"outputs": [],
"source": [
"# All digits are wrong 738\n",
"test_rule(\n",
" 3, \n",
" ((0,2,1), (0,6,9), (6,9,2), (6,0,0)), \n",
" ((0,7,0), (3,0,6), (8,2,0), (7,3,8)),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 215,
"metadata": {},
"outputs": [],
"source": [
"# One digit is right but in the wrong place 380\n",
"test_rule(\n",
" 4, \n",
" ((9,3,9), (5,5,3), (8,5,5), (0,9,9)), \n",
" ((0,0,0),(3,8,0), (0,3,8), (3,3,8)),\n",
")\n",
"# If incorrect check if (3,3,6) should be false due to \n",
"# 3 in is right in the right place as well"
]
},
{
"cell_type": "code",
"execution_count": 213,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 213,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rules[4]((3,3,6))"
]
},
{
"cell_type": "code",
"execution_count": 227,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(0, 4, 2)]"
]
},
"execution_count": 227,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[guess for guess in product(range(10), repeat=3) if validate(guess)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Only valid answer is `042` based on given ruleset"
]
}
],
"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.8.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment