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
| // npm i --save benchmark | |
| const Benchmark = require('benchmark'); | |
| var suite = new Benchmark.Suite; | |
| const addNumberFunc = (board, point, number=2)=>{ | |
| const line = board[point[0]]; | |
| return [ | |
| ...board.slice(0, point[0]), | |
| [ | |
| ...line.slice(0, point[1]), |
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 addNumber = (board, point, number=2) => { | |
| return board.map( // Iterate over columns | |
| (line, columnIdx) => line.map((block, lineIdx) => // Iterate over line | |
| // If column and line index is equals, return 2, else return the number | |
| columnIdx === point[0] && lineIdx === point[1] ? number : block | |
| )); | |
| }; |
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
| // @param 2 in format [column:int, line:int] | |
| // @param number with default value 2 | |
| const addNumber = (board, [columnIndex, lineIndex], number=2) => { | |
| const line = board[columnIndex]; // Get line to change | |
| const newLine = [ | |
| ...line.slice(0, lineIndex), // Getting all values before index to change | |
| number, // Add number to array | |
| ...line.slice(lineIndex + 1, line.length), // Getting all values after index to change | |
| ]; |
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 getRandomIndex = (zeros) => { | |
| return zeros[parseInt(Math.random() * zeros.length)]; | |
| }; |
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
| fn add_number_bad_way(board: Vec<Vec<i32>>, point: Vec<i32>, number: i32) -> Vec<Vec<i32>> { | |
| return board.iter().enumerate().map(|(column_index, line)| { | |
| return line.iter().enumerate().map(|(line_index, value)| { | |
| if column_index as i32 == point[0] && line_index as i32 == point[1] { | |
| return number; | |
| } | |
| return *value; | |
| }).collect(); | |
| }).collect(); | |
| } |
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 getEmptyBlocks = (board) => ( | |
| board.reduce((lineData, line, idxColumn) => { // Iterate over columns | |
| const result = line.reduce((data, row, idx) => { // Iterate over lines | |
| if (row !== 0) return data; // If value is different of zero, response last iterate | |
| return [[idxColumn, idx], ...data]; // Join last iterate (or default value) with new data | |
| }, []); // Default value to reduce in line | |
| return [...lineData, ...result]; // Join old value with new value and return to next reduce | |
| }, []) // Default value to reduce in column | |
| ); |
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
| // Funcion non Functional | |
| const getEmptyBlocksNonFunctional = (currentBoard) => { | |
| const positions = []; // Variable with positions | |
| for(let i = 0; i < currentBoard.length; i++) { // Iterate in columns | |
| for(let j = 0; j < currentBoard[i].length; j++) { // Iterate in lines | |
| if(currentBoard[i][j] === 0) { | |
| positions.push([i, j]); // Append in pushes when 0 | |
| } | |
| } | |
| } |
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 board = [ | |
| [0, 0, 0, 0], | |
| [0, 0, 0, 0], | |
| [0, 0, 0, 0], | |
| [0, 0, 0, 0], | |
| ]; | |
| // Important, when you see this matix in code, | |
| // you are seeing the board rotated 90 deg to left, | |
| // because the first value is column and the second value is the line | |
| // board[column, line] |
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 printBoard = (board) => { | |
| const format = (n, stringSize) => { | |
| const size = n.toString().length; | |
| return ' '.repeat(stringSize - spaces) + n + ' '; | |
| } | |
| const textToPrint = board.map((column, idxLine) => { | |
| const line = column.map((_, idxColumn) => format(board[idxColumn][idxLine])); | |
| return line.join('|'); | |
| }); | |
| console.log('-'.repeat(4*5+5)); |
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 readlineSync = require('readline-sync'); | |
| const printBoard = (board) => { | |
| const f = (n) => { | |
| const size = n.toString().length; | |
| const spaces = 5-size; | |
| return ' '.repeat(spaces) + n.toString() + ' '; | |
| }; | |
| textToPrint = []; |