Nonoblock: Difference between revisions

m
Handle the case where there is a full line.
m (Handle the case where there is a full line.)
Line 907:
 
=={{header|JavaScript}}==
<lang javascript>const mkArrcompose = (l...fn) => (...x) => fn.reduce((a, fb) => Arrayc => a(b(lc))).fill(f...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 sumsTo = val => arr => sumArr(arr) === val;
Line 914 ⟶ 917:
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(
Line 931 ⟶ 933:
};
 
const permutations = (grpSize, trgValnumGaps, minVal = 0) => {
const maxVal = trgValnumGaps - grpSize * minVal + minVal;
const sumsToTrg = sumsTo(numGaps);
const hasInnerZero = hasInner(0);
const noInnerZero = ecompose(inv, => !hasInner(0)(ehasInnerZero);
return maxVal <= 0 ? [] : (looper(mkArr(grpSize, minVal), maxVal)[1])
.filter(sumsTo(trgVal)noInnerZero)
.filter(noInnerZerosumsToTrg);
}
 
Line 941 ⟶ 946:
console.log(`\n${cells} cells. Blocks: ${blocks}`);
const grpSize = blocks.length + 1;
const targetValnumGaps = cells - sumArr(blocks);
const combinefullLine = zipArr([...blocks]numGaps === 0 && grpSize === 2);
const combine = zipArr([...blocks]);
permutations(grpSize, targetVal)
const choices = .map(toBin(choose(0, 1)));
.map(combine)
const output = compose(console.log, arrJoin(''));
.map(toBin(choose(0,1)))
const res = fullLine ? [[0, cells]].map(choices)
.map(e => console.log(e.join('')))
: permutations(grpSize, numGaps).map(compose(choices, combine));
res.map(combineoutput)
};
 
 
test(5, 2, 1);
test(5);
test(5, 5);
test(5, 1, 1, 1);
test(10, 8);
Line 959 ⟶ 966:
</lang>
{{out}}
<pre>5 cells. Blocks: 2,1
<pre>
5 cells. Blocks: 2,1
11010
11001
Line 967 ⟶ 973:
5 cells. Blocks:
00000
 
5 cells. Blocks: 5
11111
 
5 cells. Blocks: 1,1,1
Line 1,002 ⟶ 1,011:
 
5 cells. Blocks: 2,3
 
 
</pre>
Anonymous user