Created
March 22, 2018 12:03
-
-
Save vitaliiznak/05faf8a60a5b4697550d1fc63125f2f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const coordsDigitsCalcSum = coords => | |
| coords | |
| .reduce( | |
| (acc, el) => | |
| Math.abs(el) | |
| .toString(10) | |
| .split("") | |
| .map(num => Number(num)) | |
| .concat(acc), | |
| [] | |
| ) | |
| .reduce((acc, val) => acc + val, 0); | |
| function countStepsImperatively(startCoords, digitsSumLimit = 21) { | |
| const queue = []; | |
| let startCoordsDigitsSum = coordsDigitsCalcSum(startCoords); | |
| if (startCoordsDigitsSum > digitsSumLimit) return 0; | |
| let steps = 0; | |
| queue.push(startCoords); | |
| const processedNodes = { [startCoords]: [startCoords] }; | |
| while (queue.length > 0) { | |
| let coords = queue.pop(); | |
| ++steps; | |
| coords.forEach((coord, index, coordsArray) => { | |
| const newCoordsPlus = coordsArray.slice(); | |
| newCoordsPlus[index] = coord + 1; | |
| let coordsPlusDigitsSum = coordsDigitsCalcSum(newCoordsPlus); | |
| if ( | |
| coordsPlusDigitsSum <= digitsSumLimit && | |
| !processedNodes[newCoordsPlus] | |
| ) { | |
| queue.push(newCoordsPlus); | |
| processedNodes[newCoordsPlus] = newCoordsPlus; | |
| } | |
| const newCoordsMinus = coordsArray.slice(); | |
| newCoordsMinus[index] = coord - 1; | |
| let coordsMinusDigitsSum = coordsDigitsCalcSum(newCoordsMinus); | |
| if ( | |
| coordsMinusDigitsSum <= digitsSumLimit && | |
| !processedNodes[newCoordsMinus] | |
| ) { | |
| queue.push(newCoordsMinus); | |
| processedNodes[newCoordsMinus] = newCoordsMinus; | |
| } | |
| }); | |
| } | |
| return steps; | |
| } | |
| function countStepsRecursively( | |
| coords, | |
| digitsSumLimit = 21, | |
| processedNodes = {} | |
| ) { | |
| if (processedNodes[coords]) { | |
| return 0; | |
| } | |
| processedNodes[coords] = coords; | |
| const digitsSum = coordsDigitsCalcSum(coords); | |
| if (digitsSum > digitsSumLimit) { | |
| return 0; | |
| } | |
| const [x, y] = coords; | |
| return ( | |
| 1 + | |
| coords | |
| .map((coord, index, coordsArray) => { | |
| const newCoordsPlus = coordsArray.slice(); | |
| newCoordsPlus[index] = coord + 1; | |
| const newCoordsMinus = coordsArray.slice(); | |
| newCoordsMinus[index] = coord - 1; | |
| return ( | |
| (processedNodes[newCoordsPlus] | |
| ? 0 | |
| : countStepsRecursively( | |
| newCoordsPlus, | |
| digitsSumLimit, | |
| processedNodes | |
| )) + | |
| (processedNodes[newCoordsMinus] | |
| ? 0 | |
| : countStepsRecursively( | |
| newCoordsMinus, | |
| digitsSumLimit, | |
| processedNodes | |
| )) | |
| ); | |
| }) | |
| .reduce((acc, val) => acc + val, 0) | |
| ); | |
| } | |
| console.log( | |
| "countStepsImperatively([0,0], 1) gives", | |
| countStepsImperatively([0], 1) | |
| ); | |
| //gives 3, trivial points with coordinates [0],[-1],[1] | |
| console.log( | |
| "countStepsImperatively([0,0], 1) gives", | |
| countStepsImperatively([0, 0], 1) | |
| ); | |
| //gives 3, trivial points with coordinate [0,0], [0,1], [0,-1], [-1,0] | |
| console.log( | |
| "countStepsImperatively([0,0], 21) gives", | |
| countStepsImperatively([0, 0], 21) | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment