Nonoblock: Difference between revisions
Content added Content deleted
m (Handle the case where there is a full line.) |
|||
Line 907: | Line 907: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
<lang javascript>const |
<lang javascript>const compose = (...fn) => (...x) => fn.reduce((a, b) => c => a(b(c)))(...x); |
||
const inv = b => !b; |
|||
const arrJoin = str => arr => arr.join(str); |
|||
const mkArr = (l, f) => Array(l).fill(f); |
|||
const sumArr = arr => arr.reduce((a, b) => a + b, 0); |
const sumArr = arr => arr.reduce((a, b) => a + b, 0); |
||
const sumsTo = val => arr => sumArr(arr) === val; |
const sumsTo = val => arr => sumArr(arr) === val; |
||
Line 914: | Line 917: | ||
const zipArr = arr => a => zip(a, arr); |
const zipArr = arr => a => zip(a, arr); |
||
const hasInner = v => arr => arr.slice(1, -1).indexOf(v) >= 0; |
const hasInner = v => arr => arr.slice(1, -1).indexOf(v) >= 0; |
||
⚫ | |||
const choose = (even, odd) => n => n % 2 === 0 ? even : odd; |
const choose = (even, odd) => n => n % 2 === 0 ? even : odd; |
||
const toBin = f => arr => arr.reduce( |
const toBin = f => arr => arr.reduce( |
||
Line 931: | Line 933: | ||
}; |
}; |
||
const permutations = (grpSize, |
const permutations = (grpSize, numGaps, minVal = 0) => { |
||
const maxVal = |
const maxVal = numGaps - grpSize * minVal + minVal; |
||
const sumsToTrg = sumsTo(numGaps); |
|||
const hasInnerZero = hasInner(0); |
|||
⚫ | |||
return maxVal <= 0 ? [] : (looper(mkArr(grpSize, minVal), maxVal)[1]) |
return maxVal <= 0 ? [] : (looper(mkArr(grpSize, minVal), maxVal)[1]) |
||
.filter( |
.filter(noInnerZero) |
||
.filter( |
.filter(sumsToTrg); |
||
} |
} |
||
Line 941: | Line 946: | ||
console.log(`\n${cells} cells. Blocks: ${blocks}`); |
console.log(`\n${cells} cells. Blocks: ${blocks}`); |
||
const grpSize = blocks.length + 1; |
const grpSize = blocks.length + 1; |
||
const |
const numGaps = cells - sumArr(blocks); |
||
const |
const fullLine = (numGaps === 0 && grpSize === 2); |
||
const combine = zipArr([...blocks]); |
|||
permutations(grpSize, targetVal) |
|||
⚫ | |||
⚫ | |||
const output = compose(console.log, arrJoin('')); |
|||
⚫ | |||
const res = fullLine ? [[0, cells]].map(choices) |
|||
.map(e => console.log(e.join(''))) |
|||
: permutations(grpSize, numGaps).map(compose(choices, combine)); |
|||
⚫ | |||
}; |
}; |
||
test(5, 2, 1); |
test(5, 2, 1); |
||
test(5); |
test(5); |
||
test(5, 5); |
|||
test(5, 1, 1, 1); |
test(5, 1, 1, 1); |
||
test(10, 8); |
test(10, 8); |
||
Line 959: | Line 966: | ||
</lang> |
</lang> |
||
{{out}} |
{{out}} |
||
⚫ | |||
<pre> |
|||
⚫ | |||
11010 |
11010 |
||
11001 |
11001 |
||
Line 967: | Line 973: | ||
5 cells. Blocks: |
5 cells. Blocks: |
||
00000 |
00000 |
||
5 cells. Blocks: 5 |
|||
11111 |
|||
5 cells. Blocks: 1,1,1 |
5 cells. Blocks: 1,1,1 |
||
Line 1,002: | Line 1,011: | ||
5 cells. Blocks: 2,3 |
5 cells. Blocks: 2,3 |
||
</pre> |
</pre> |