Skip to content

Instantly share code, notes, and snippets.

@grahamannett
Created September 28, 2018 01:20
Show Gist options
  • Save grahamannett/5c23b5678e2229172eb69fc47e4e7afd to your computer and use it in GitHub Desktop.
Save grahamannett/5c23b5678e2229172eb69fc47e4e7afd to your computer and use it in GitHub Desktop.
import numpy as np
import itertools
keypad = [list(range(n, n+3)) for n in range(1, 10, 3)]
keypad.append([None, 0, None])
keypad = np.array(keypad)
def get_values(obs):
x, y = np.where(keypad == obs)
x, y = x[0], y[0]
possibilities = []
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if i + j not in [-1, 1] or 0 > x + i < 2 or 0 > y + j < 2:
continue
try:
possibilities.append(keypad[x + i, y + j])
except IndexError:
continue
return [n for n in possibilities if n is not None]
def get_pins(*obs):
return sorted(itertools.product(*[get_values(o) for o in obs]))
print(get_pins(5,2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment