Advertisement
pahosler

solvable.dart

May 19th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.52 KB | None | 0 0
  1. void main() {
  2.   List<int> board = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
  3.   isSolvable(board) ? print("Solvable") : print("Not Solvable");
  4. }
  5.  
  6. int getInvCount(List<int> arr){
  7.   int inv_count = 0;
  8.   int N = arr.length;
  9.   for(int i = 0; i < N -1; i++){
  10.     for (int j = i +(i * 4) + 1; j < N; j++){
  11.       if (arr[j] && arr[i] && arr[i] > arr[j])
  12.         inv_count++;
  13.     }
  14.   }
  15.   return inv_count;
  16. }
  17.  
  18. int findXPosition(List<int> arr) {
  19.   int N = arr.length;
  20.   for(int i = N-1; i >= 0; i--)
  21.     for(int j = N-1; j>=0; j--)
  22.       if(arr[i+(j * 4)] == 0)
  23.         return N - i;
  24. }
  25.  
  26. bool isSolvable(List<int> puzzle){
  27.   int N = puzzle.length;
  28.   int inv_count = getInvCount(puzzle);
  29.   if(N & 1)
  30.     return !(inv_count & 1);
  31.   else {
  32.     int pos = findXPosition(puzzle);
  33.     if(pos & 1)
  34.       return !(inv_count & 1);
  35.     else
  36.       return inv_count & 1;
  37.   }
  38. }
  39.  
  40. /*
  41.      screen full of errors
  42.  
  43. hello.dart:13:14: Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
  44. Try changing the type of the left hand side, or casting the right hand side to 'bool'.
  45.       if (arr[j] && arr[i] && arr[i] > arr[j])
  46.              ^
  47. hello.dart:13:24: Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
  48. Try changing the type of the left hand side, or casting the right hand side to 'bool'.
  49.       if (arr[j] && arr[i] && arr[i] > arr[j])
  50.                        ^
  51. hello.dart:31:8: Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
  52. Try changing the type of the left hand side, or casting the right hand side to 'bool'.
  53.   if(N & 1)
  54.        ^
  55. hello.dart:32:24: Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
  56. Try changing the type of the left hand side, or casting the right hand side to 'bool'.
  57.     return !(inv_count & 1);
  58.                        ^
  59. hello.dart:35:12: Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
  60. Try changing the type of the left hand side, or casting the right hand side to 'bool'.
  61.     if(pos & 1)
  62.            ^
  63. hello.dart:36:26: Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
  64. Try changing the type of the left hand side, or casting the right hand side to 'bool'.
  65.       return !(inv_count & 1);
  66.                          ^
  67. hello.dart:38:24: Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
  68. Try changing the type of the left hand side, or casting the right hand side to 'bool'.
  69.       return inv_count & 1;
  70.                        ^
  71. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement