Created
July 13, 2020 02:23
-
-
Save killedbymemory/bfa12ad728ba00620dc96d95b6172eb8 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
let RIGHT = 'right'; | |
let BOTTOM = 'bottom'; | |
let LEFT = 'left'; | |
let TOP = 'top'; | |
function createMatrix(numOfRows, numOfColumns) { | |
return Array | |
.from({length: numOfRows}) | |
.map(row => Array.from({length: numOfColumns}).map(_ => 0)); | |
} | |
function createStepper(matrix, numOfRows, numOfColumns, initialDirection) { | |
let currentRowIndex = 0; | |
let currentColumnIndex = 0; | |
let currentValue = 1; | |
let currentDirection = initialDirection; | |
return function step() { | |
if (currentDirection === RIGHT) { | |
if (matrix[currentRowIndex][currentColumnIndex + 1] === 0) { | |
nextRowIndex = currentRowIndex; | |
nextColumnIndex = currentColumnIndex + 1; | |
nextDirection = currentDirection; | |
nextValue = currentValue; | |
} else { | |
nextRowIndex = currentRowIndex + 1; | |
nextColumnIndex = currentColumnIndex; | |
nextDirection = BOTTOM; | |
nextValue = currentValue + 1; | |
} | |
} | |
if (currentDirection === BOTTOM) { | |
if (matrix[currentRowIndex + 1] && matrix[currentRowIndex + 1][currentColumnIndex] === 0) { | |
nextRowIndex = currentRowIndex + 1; | |
nextColumnIndex = currentColumnIndex; | |
nextDirection = currentDirection; | |
nextValue = currentValue; | |
} else { | |
nextRowIndex = currentRowIndex; | |
nextColumnIndex = currentColumnIndex - 1; | |
nextDirection = LEFT; | |
nextValue = currentValue + 1; | |
} | |
} | |
if (currentDirection === LEFT) { | |
if (matrix[currentRowIndex][currentColumnIndex - 1] === 0) { | |
nextRowIndex = currentRowIndex; | |
nextColumnIndex = currentColumnIndex - 1; | |
nextDirection = currentDirection; | |
nextValue = currentValue; | |
} else { | |
nextRowIndex = currentRowIndex - 1; | |
nextColumnIndex = currentColumnIndex; | |
nextDirection = TOP; | |
nextValue = currentValue + 1; | |
} | |
} | |
if (currentDirection === TOP) { | |
if (matrix[currentRowIndex - 1] && matrix[currentRowIndex - 1][currentColumnIndex] === 0) { | |
nextRowIndex = currentRowIndex - 1; | |
nextColumnIndex = currentColumnIndex; | |
nextDirection = currentDirection; | |
nextValue = currentValue; | |
} else { | |
nextRowIndex = currentRowIndex; | |
nextColumnIndex = currentColumnIndex + 1; | |
nextDirection = RIGHT; | |
nextValue = currentValue + 1; | |
} | |
} | |
const current = [currentRowIndex, currentColumnIndex, currentValue]; | |
const next = [nextRowIndex, nextRowIndex, nextValue]; | |
currentRowIndex = nextRowIndex; | |
currentColumnIndex = nextColumnIndex; | |
currentDirection = nextDirection; | |
currentValue = nextValue; | |
return [current, next]; | |
} | |
} | |
function circular(numOfRows = 1, numOfColumns = 5, direction = RIGHT) { | |
const matrix = createMatrix(numOfRows, numOfColumns); | |
const length = numOfRows * numOfColumns; | |
const step = createStepper(matrix, numOfRows, numOfColumns, direction); | |
for (var i = 0; i < length; i++) { | |
var [current, next] = step(); | |
var [rowIndex, columnIndex, value] = current; | |
matrix[rowIndex][columnIndex] = value; | |
} | |
return matrix; | |
} | |
function assert(inputMatrix, expectedMatrix, debug = false) { | |
const result = inputMatrix.every((row, rowIndex) => { | |
debug && console.log({row, rowIndex}); | |
return row.every((value, cellIndex) => { | |
debug && console.log({ | |
value, | |
cellIndex, | |
expectedValue: expectedMatrix[rowIndex][cellIndex], | |
}); | |
return value === expectedMatrix[rowIndex][cellIndex]; | |
}); | |
}); | |
if (result) { | |
return true; | |
} | |
return {expected: expectedMatrix, found: inputMatrix}; | |
} | |
console.log(assert(circular(1, 5), [[1,1,1,1,1]])); | |
console.log(assert(circular(2, 5), [ | |
[1,1,1,1,1], | |
[3,3,3,3,2] | |
])); | |
console.log(assert(circular(3, 5), [ | |
[1,1,1,1,1], | |
[4,5,5,5,2], | |
[3,3,3,3,2] | |
])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment