Nonoblock: Difference between revisions

(alphabetize, minor clean-up)
Line 904:
blocks [2, 3], cells 5
No solution</pre>
 
 
=={{header|JavaScript}}==
<lang javascript>const mkArr = (l, f) => Array(l).fill(f);
const sumArr = arr => arr.reduce((a, b) => a + b, 0);
const sumsTo = val => arr => sumArr(arr) === val;
const zipper = arr => (p, c, i) => arr[i] ? [...p, c, arr[i]] : [...p, c];
const zip = (a, b) => a.reduce(zipper(b), []);
const zipArr = arr => a => zip(a, arr);
const hasInner = v => arr => arr.slice(1, -1).indexOf(v) >= 0;
const noInnerZero = e => !hasInner(0)(e);
const choose = (even, odd) => n => n % 2 === 0 ? even : odd;
const toBin = f => arr => arr.reduce(
(p, c, i) => [...p, ...mkArr(c, f(i))], []);
 
 
const looper = (arr, max, acc = [[...arr]], idx = 0) => {
if (idx !== arr.length) {
const b = looper([...arr], max, acc, idx + 1)[0];
if (b[idx] !== max) {
b[idx] = b[idx] + 1;
acc.push(looper([...b], max, acc, idx)[0]);
}
}
return [arr, acc];
};
 
const permutations = (grpSize, trgVal, minVal = 0) => {
const maxVal = trgVal - grpSize * minVal + minVal;
return maxVal <= 0 ? [] : (looper(mkArr(grpSize, minVal), maxVal)[1])
.filter(sumsTo(trgVal))
.filter(noInnerZero);
}
 
const test = (cells, ...blocks) => {
console.log(`\n${cells} cells. Blocks: ${blocks}`);
const grpSize = blocks.length + 1;
const targetVal = cells - sumArr(blocks);
const combine = zipArr([...blocks])
permutations(grpSize, targetVal)
.map(combine)
.map(toBin(choose(0,1)))
.map(e => console.log(e.join('')))
};
 
 
test(5, 2, 1);
test(5);
test(5, 1, 1, 1);
test(10, 8);
test(15, 2, 3, 2, 3);
test(10, 4, 3);
test(5, 2, 3);
</lang>
<pre>
5 cells. Blocks: 2,1
11010
11001
01101
 
5 cells. Blocks:
00000
 
5 cells. Blocks: 1,1,1
10101
 
10 cells. Blocks: 8
1111111100
0111111110
0011111111
 
15 cells. Blocks: 2,3,2,3
110111011011100
110111011001110
110111011000111
110111001101110
110111001100111
110111000110111
110011101101110
110011101100111
110011100110111
110001110110111
011011101101110
011011101100111
011011100110111
011001110110111
001101110110111
 
10 cells. Blocks: 4,3
1111011100
1111001110
1111000111
0111101110
0111100111
0011110111
 
5 cells. Blocks: 2,3
 
</pre>
 
=={{header|Julia}}==
Anonymous user