Advertisement
Guest User

Untitled

a guest
May 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. tests = [
  2. {
  3. expected: 'win',
  4. maze: ['.'],
  5. steps: 0
  6. },
  7.  
  8. {
  9. expected: 'lose',
  10. maze: [
  11. '.#',
  12. '#.'
  13. ],
  14. steps: 2
  15. },
  16.  
  17. {
  18. expected: 'win',
  19. maze: [
  20. '..',
  21. '#.'
  22. ],
  23. steps: 2
  24. },
  25.  
  26. {
  27. expected: 'win',
  28. maze: [
  29. '.##',
  30. '..#',
  31. '#..'
  32. ],
  33. steps: 4
  34. },
  35.  
  36. {
  37. expected: 'lose',
  38. maze: [
  39. '..#',
  40. '#.#',
  41. '##.'
  42. ],
  43. steps: 3
  44. },
  45.  
  46. {
  47. expected: 'win',
  48. maze: [
  49. '.#...',
  50. '...#.',
  51. ],
  52. steps: 7
  53. },
  54.  
  55. {
  56. expected: 'lose',
  57. maze: [
  58. '.#...',
  59. '...#.',
  60. ],
  61. steps: 6
  62. },
  63.  
  64. {
  65. expected: 'win',
  66. maze: [
  67. '.#...',
  68. '...#.',
  69. '####.',
  70. '.....',
  71. '.#.##',
  72. '.....'
  73. ],
  74. steps: 15
  75. },
  76.  
  77. {
  78. expected: 'lose',
  79. maze: [
  80. '.#...',
  81. '...#.',
  82. '####.',
  83. '.....',
  84. '.####',
  85. '.....'
  86. ],
  87. steps: 15
  88. },
  89.  
  90. {
  91. expected: 'lose',
  92. maze: [
  93. '..',
  94. '#.',
  95. '..',
  96. '.#',
  97. ],
  98. steps: 5
  99. },
  100.  
  101. {
  102. expected: 'win',
  103. maze: [
  104. '..',
  105. '#.',
  106. '..',
  107. '.#',
  108. '..'
  109. ],
  110. steps: 7
  111. },
  112.  
  113. {
  114. expected: 'win',
  115. maze: [
  116. '..###########',
  117. '#..........#',
  118. '#..........#',
  119. '#..........#',
  120. '#..........#',
  121. '#..........#',
  122. '#..........#',
  123. '#..........#',
  124. '#..........#',
  125. '#..........#',
  126. '#..........#',
  127. '##########..'
  128. ],
  129. steps: 23
  130. },
  131.  
  132. {
  133. expected: 'win',
  134. maze: [
  135. '..###########',
  136. '#..........#',
  137. '#..........#',
  138. '#..........#',
  139. '#..........#',
  140. '#..........#',
  141. '#..........#',
  142. '#..........#',
  143. '#..........#',
  144. '#..........#',
  145. '#..........#',
  146. '#####.######',
  147. '...........#',
  148. '##########..'
  149. ],
  150. steps: 35
  151. },
  152.  
  153. ];
  154.  
  155. function Position(row, col) {
  156. this.row = row;
  157. this.col = col;
  158. this.equals = function(position) {
  159. if (position.row == this.row && position.col == this.col) {
  160. return true;
  161. } else {
  162. return false;
  163. }
  164. };
  165. this.getRight = function() {
  166. return new Position(this.row, this.col+1);
  167. };
  168. this.getDown = function() {
  169. return new Position(this.row+1, this.col);
  170. };
  171. this.getLeft = function() {
  172. return new Position(this.row, this.col-1);
  173. };
  174. this.getUp = function() {
  175. return new Position(this.row-1, this.col);
  176. };
  177. }
  178.  
  179.  
  180. function isValidPosition(history, maze, position) {
  181. //console.log(2, position);
  182. // it's negative
  183. if (position.row < 0 || position.col < 0) {
  184. return false;
  185. }
  186. // out of bounds
  187. if (position.row > maze.length-1 || position.col > maze[0].length-1) {
  188. return false;
  189. }
  190. // it's a hedge
  191. const row = maze[position.row];
  192. if (row[position.col] === '#') {
  193. return false
  194. }
  195. // we've been there before
  196. const beenThere = history.find(p => position.equals(p)) ? true : false;
  197. if (beenThere) {
  198. return false;
  199. }
  200.  
  201. return true;
  202. }
  203.  
  204. function go(history, maze, position, steps) {
  205. //console.log(1, position);
  206. // can we even go here?
  207. if (!isValidPosition(history, maze, position)) {
  208. return false;
  209. }
  210. // is the position the winner? did we take fewer steps?
  211. const winningPosition = new Position(maze.length-1, maze[0].length-1);
  212. if (position.equals(winningPosition) && history.length <= steps) {
  213. return true;
  214. }
  215.  
  216. const newHistory = [...history, position];
  217.  
  218. // go in every direction!
  219. return (
  220. go(newHistory, maze, position.getRight(), steps) ||
  221. go(newHistory, maze, position.getDown(), steps) ||
  222. go(newHistory, maze, position.getLeft(), steps) ||
  223. go(newHistory, maze, position.getUp(), steps)
  224. );
  225. }
  226.  
  227. function solveTheMaze(maze, steps) {
  228. // you can tred on . but not #
  229. // you always start in the upper left
  230. // you have to get to the bottom right in $steps or fewer steps
  231. // if you can, return 'win', otherwise 'lose'
  232. // ###############################################################
  233.  
  234. const history = new Array();
  235. const currentPosition = new Position(0,0);
  236. //console.log(0, currentPosition);
  237.  
  238. if (go(history, maze, currentPosition, steps)) {
  239. return 'win';
  240. } else {
  241. return 'lose';
  242. }
  243.  
  244. // stub
  245. //return 'lose';
  246. }
  247.  
  248. tests.forEach((test, index) => {
  249. result = solveTheMaze(test.maze, test.steps);
  250. console.log(index, result === test.expected ? 'pass' : 'fail');
  251. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement