public static int getTurns ( final AdjacencyGraph adjacencyGraph , final String part ) { int direction = 0 ; int turns = 1 ; char [ ] parts = part . toCharArray ( ) ; for ( int i1 = 0 ; i1 < parts . length ; i1 ++ ) { Character character = parts [ i1 ] ; if ( i1 + 1 >= parts . length ) { continue ; } Character next_character = parts [ i1 + 1 ] ; if ( adjacencyGraph . getKeyMap ( ) . containsKey ( character ) ) { String [ ] tmp_neighbors = adjacencyGraph . getKeyMap ( ) . get ( character ) ; for ( int i2 = 0 ; i2 < tmp_neighbors . length ; i2 ++ ) { if ( tmp_neighbors [ i2 ] == null ) { continue ; } for ( Character neighbor_char : tmp_neighbors [ i2 ] . toCharArray ( ) ) { if ( next_character . equals ( neighbor_char ) ) { if ( direction == 0 ) { direction = i2 ; } else if ( direction != i2 ) { turns ++ ; direction = i2 ; } } } } } } return turns ; }
Returns the number of turns in the part passed in based on the adjacency graph .