Skip to content

Instantly share code, notes, and snippets.

View manvillej's full-sized avatar

Jeff Manville manvillej

View GitHub Profile
@manvillej
manvillej / knight.py
Created October 9, 2018 04:12
Knight to E4
'''
So, I started off by visualizing the network map
1 - 6 - 7
| | |
8 0 2
| | |
3 - 4 - 9
5
@manvillej
manvillej / transition
Last active November 17, 2018 01:57
Defining the transition Matrix
>>> transitionMatrix = np.matrix(
[[0, 0, 0, 0, 1, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 1],
[0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
@manvillej
manvillej / stateVector
Last active October 20, 2018 18:22
State Vector
>>> stateVector
matrix([[0, 1, 0, 0, 0, 0, 0, 0, 0, 0]])
@manvillej
manvillej / nextState.py
Last active November 14, 2018 01:56
Next State Matrix Multiplication
transitionMatrix = np.matrix([[0, 0, 0, 0, 1, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 1],
[0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0, 0, 0, 0]], dtype=object)
@manvillej
manvillej / twoturns.py
Last active October 20, 2018 18:20
2 turns forward from number 1
>>> nextState = nextState * transitionMatrix
>>> nextState
matrix([[1, 2, 0, 1, 0, 0, 0, 1, 0, 0]])
>>> np.sum(nextState)
5
@manvillej
manvillej / getMatrix.py
Last active November 13, 2018 19:53
version 1 of get matrix function
>>> import numpy as np
>>> def getMatrix(matrix, turns):
... newMatrix = np.identity(matrix.shape[0])
... for i in range(turns):
... newMatrix = newMatrix*matrix
... return newMatrix
>>> twoTurnMatrix = getMatrix(transitionMatrix,2)
>>> twoTurnMatrix
matrix([
@manvillej
manvillej / SumStateVector.py
Last active October 23, 2018 13:10
summing a row in a matrix.
>>> matrix
matrix([[ 2., 0., 1., 0., 0., 0., 1., 0., 0., 1.],
[ 0., 2., 0., 1., 0., 1., 0., 0., 0., 0.],
[ 1., 0., 2., 0., 0., 0., 0., 0., 1., 1.],
[ 0., 1., 0., 3., 0., 1., 0., 1., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 1., 0., 1., 0., 3., 0., 1., 0., 0.],
[ 1., 0., 0., 0., 0., 0., 2., 0., 1., 1.],
[ 0., 0., 0., 1., 0., 1., 0., 2., 0., 0.],
[ 0., 0., 1., 0., 0., 0., 1., 0., 2., 1.],
@manvillej
manvillej / KnightSolution1.py
Last active November 17, 2018 02:11
Our first solution to the Knight Dialer
import numpy as np
def getMatrix(matrix, turns):
newMatrix = np.identity(matrix.shape[0])
for i in range(turns):
newMatrix = newMatrix*matrix
return newMatrix
def getPossibilities(startingPosition, turns):
@manvillej
manvillej / neighbor.py
Created November 13, 2018 19:45
Neighbors if the Knight Dialer
NEIGHBORS_MAP = {
1: (6, 8),
2: (7, 9),
3: (4, 8),
4: (3, 9, 0),
5: tuple(), # 5 has no neighbors
6: (1, 7, 0),
7: (2, 6),
8: (1, 3),
9: (2, 4),
@manvillej
manvillej / mapPosition.py
Created November 17, 2018 03:58
mapping a starting position for the old matrix to a new position for the new matrix
def getMappedStartingPosition(startingPosition):
"""
maps original starting positions to starting positions on the condensed matrix
"""
mappingDictionary = {
0:3,
1:0,
2:1,
3:0,
4:2,