2

I will describe the algorithm in question with an example:

I have a piece, lets say piece number 7.

I have a 4 x 4 sized puzzle with id's

00 01 02 03
10 11 12 13
20 21 22 23
30 31 32 33

I need to determine the correct location for piece 7

The location would be 12

I'm writing this in python and think I could use a bruit force method. I've tried counting up and reaching the location I need.

Any ideas for a simple algorithm? The output needs to to be the same since I'm working with legacy code.

visc
  • 4,794
  • 6
  • 32
  • 58

3 Answers3

0

REVISED

use a modulo and a division operation to find the x and y coordinates the whole number of the piecenumber/columns is the x coord the whole number of the piecenumber%columns is the y coord #i.e. the remainder of the piecenumber/columns

NOTE: remember that rows and columns in python start with 0 not with 1. so the third row is actually row number 2. the tile in coordinate 0,0 is number 1 so you subtract 1 from the y coordinate. Therefore your algorithm is this:

valueyouwant=grid[tilenumber/numberofcolumns][tilenumber%numberofcolumns-1]

i.e. the 34th tile on a 6x7 grid=

34/6 =5 (ignore remainder)
34%6 =4 -1 = 3

x=5
y=3

00 01 02 03 04 05
10 11 12 13 14 15
20 21 22 23 24 25
30 31 32 33 34 35
40 41 42 43 44 45
50 51 52 53 54 55
60 61 62 63 64 65

Answer is 53

or for you example:

piecenum=7
row=piecenum/4
column=piecenum%4-1
value you want = puzzle[row][column] 
Amazingred
  • 1,007
  • 6
  • 14
  • so if we have a 5*9 and we want to place piece 11. the answer should be 20. If we want to place piece 17 the answer would be 31. – visc Apr 27 '14 at 18:43
  • It isn't always square. And sorry I wasn't clear. Its not about placing the piece not the matrix it is about get the id of the location of the grid. – visc Apr 27 '14 at 18:44
  • 1
    how is grid[tilenumber/columns][tilenumber%columns-1] not pythonic? – Amazingred Apr 27 '14 at 19:19
0

What you need is python list comprehension in purpose of flattening a nested list. :)

And that's how it's done:

data1.txt

00 01 02 03
10 11 12 13
20 21 22 23
30 31 32 33

data2.txt

00 01 02 03 04
10 11 12 13 14
20 21 22 23 24
30 31 32 33 34
40 41 42 43 44
50 51 52 53 54
60 61 62 63 64
70 71 72 73 74
80 81 82 83 84

acquire_location.py

def acquire_location_id(data, piece):
    return data[piece - 1]


def read_file(filename):
    data = []

    with open(filename) as f:
        lines = f.readlines()
        for line in lines:
            data.append([str(x) for x in line.split()])

    return [x for sublist in data for x in sublist]


data = read_file('data1.txt')
print acquire_location_id(data, 7)
# 12

data = read_file('data2.txt')
print acquire_location_id(data, 11)
# 20

print acquire_location_id(data, 17)
# 31
Community
  • 1
  • 1
Bruno Gelb
  • 5,322
  • 8
  • 35
  • 50
  • 1
    Not exactly what I was looking for, but its interesting nonetheless. Thanks :) +1 – visc Apr 28 '14 at 12:10
0

About get id of x,y location: Id=y*width+x=y*4+c in this case

Gwenc37
  • 2,064
  • 7
  • 18
  • 22