Beispiel #1
0
 def __init__(self, data):
     if isinstance(data, list):
         data = utils.matrix2dict(data)
     self.data = data
     self.rows = len(set(r for r, c in data))
     self.cols = len(set(c for r, c in data))
     self.values = sorted(set(data.values()))
     
     self.begin = (0, 0)
     self.end = (self.rows-1, self.cols-1)
     
     self.next_value = dict(zip(self.values, self.values[1:] + self.values[:1]))
     
     self.grid = data        
     self.graph = dict((rc, self.get_next(*rc)) for rc in self.grid)
Beispiel #2
0
 def parse(self, lines):
     data = parser.parse_grid(lines)
     
     m = utils.dict2matrix(data)
     d = [row[::2] for row in m[::2]]
     
     twist = Twist(len(d), len(d[0]), utils.matrix2dict(d))
     markers = {
         "|": [0, -1, 0, 1],
         "-": [-1, 0, 1, 0],
         "\\": [-1, -1, 1, 1],
         "/": [1, -1, -1, 1]
     }
     
     connections = []
     for (y, x), v in sorted(data.items()):
         if v in markers:
             dx1, dy1, dx2, dy2 = markers[v]
             x1, y1 = (x+dx1)/2, (y+dy1)/2
             x2, y2 = (x+dx2)/2, (y+dy2)/2
             c = (y1, x1), (y2, x2)
             connections.append(c)
            
     return TwistSolution(twist, connections)