diff --git a/path-finder-number-1-can-you-reach-the-exit/index.js b/path-finder-number-1-can-you-reach-the-exit/index.js new file mode 100644 index 0000000..f5eb235 --- /dev/null +++ b/path-finder-number-1-can-you-reach-the-exit/index.js @@ -0,0 +1,22 @@ +// https://www.codewars.com/kata/path-finder-number-1-can-you-reach-the-exit/train/javascript + +function pathFinder(maze) { + let m = maze.split(/\n/).map(row=>row.split``); + let h = m.length; + let w = m[0].length; + let mark = (xx, yy) => { + [[1, 0], [-1, 0], [0, 1], [0, -1], [0, 0]].map(p => { + let [x, y] = [p[0]+xx, p[1]+yy]; + if (0 <= x && x < w) { + if (0 <= y && y < h) { + if (m[y][x] === '.') { + m[y][x] = '#'; + mark(x, y); + } + } + } + }); + }; + mark(0, 0); + return m[h-1][w-1] !== '.'; +}