If you are no familiar with dominos: you have gaming pieces with two numbers on them. You have to arrange them so, that the ending of the first piece is the beginning of the second piece. So if we have 3 dominos with values [3-1][3-4][1-4] you could arrange them to [1-3], [3-4], [4-1] or [4-1], [1-3], [3-4], since you can switch the side you start with (turning the pice).
Now to my code problem:
I got a pandas df with the domino values:
| # | D1 | D2 |
|---|---|---|
| 1 | 1 | 5 |
| 2 | 2 | 4 |
| 3 | 3 | 6 |
| 4 | 4 | 1 |
| 5 | 3 | 4 |
| 6 | 2 | 3 |
| 7 | 6 | 5 |
dominos = {'D1': [1, 2,3,4,3,2,6], 'D2': [5,4,6,1,4,3,5]}
Now if I choose one of the pieces at random and need to find a 'route' to get to that specific domino (the chosen one cant be rotated), starting from 1 and ending again at 1, while using the smallest amount possible of domino pieces.
For example: the chosen piece was [3-4], the code needs to print out the route, looking something like this(or in an array):
1: #1 [1-5]
2: !#7 [5-6]
3: !#3 [6-3]
4: **[3-4]**
5: #4 [4-1]
So far I've tried to do this with a lot of for loops, which didn't work.
import pandas as pd
dominos = {'D1': [1, 2,3,4,3,2,6], 'D2': [5,4,6,1,4,3,5]}
data = pd.DataFrame(dominos)
ranD = data.loc[4,]
def findpath(data, ranD):
trargetD1 = ranD.D1
#fid possible start
for x in range(len(data)):
if data.loc[x,'D1'] == 1:
start = start + data.loc[x,]
if start.loc[x, 'D2'] == ranD.D1:
PerfectStart = start.loc[x, 'D2']
elif start.loc[x, 'D2'] != ranD.D1:
PerfectStart = start.loc[1,]
else:
print("no start found")
# when a start was found, that doesn't match ranD at pos. D2 find possible steps
for x in range(len(data)):
if PerfectStart.D2 == ranD.D2 :
step1 = step1 + data.loc[x,'D1']
if step1.loc[x, 'D2'] == ranD.D1:
PerfectStep = step1.loc[x, 'D2']
elif start.loc[x, 'D2'] != ranD.D1:
PerfectStep = step1.loc[1,]
etc.
If anyone has ideas on how to approach this, please let me know.
Your feedback is much appreciated!