You are viewing a single comment's thread. Return to all comments →
I don't understan why for n = 3, m = 2 the solution is 7, the possible solutions are without repeats, with my code in javascript:
function legoBlocks(n, m) { const combinations = summa([], m); return buildFigure(combinations, [], n) } function buildFigure(combinations, figure, n) { if (figure.length < n) { return combinations.reduce((prev, element) => { if (figure.length > 0 && figure[figure.length - 1].length > 1 && element.length > 1) { for(let j = 0; j < figure[figure.length - 1].length; j++) { if (Array.from(figure[figure.length - 1])[j] === Array.from(element)[j]) { return prev } } } return prev + buildFigure(combinations, figure.concat(element), n) }, 0) } else { //figure.forEach(row => console.log(row)) return 1; } } function summa(row, m) { const tempSumma = row.reduce((prev, block) => prev + block, 0); if (tempSumma < m) { //console.log(`Is little ${row} - ${tempSumma}`) return [1, 2, 3, 4].flatMap(newBlock => { if (tempSumma + newBlock <= m) { return summa(row.concat([newBlock]), m) } else return '' }).filter(row => row.length > 0) } else if (tempSumma === m) { //console.log(`${row} - ${m}`) return row.join(' ') } else { return '' } }
1 1 2 1 1 1 1 2 2 2 1 1 2 2 2 1 1 2 2 2
Seems like cookies are disabled on this browser, please enable them to open this website
Lego Blocks
You are viewing a single comment's thread. Return to all comments →
I don't understan why for n = 3, m = 2 the solution is 7, the possible solutions are without repeats, with my code in javascript: