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 mkArr = (l, f) => Array(l).fill(f);
<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 noInnerZero = e => !hasInner(0)(e);
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, trgVal, minVal = 0) => {
const permutations = (grpSize, numGaps, minVal = 0) => {
const maxVal = trgVal - grpSize * minVal + minVal;
const maxVal = numGaps - grpSize * minVal + minVal;
const sumsToTrg = sumsTo(numGaps);
const hasInnerZero = hasInner(0);
const noInnerZero = compose(inv, hasInnerZero);
return maxVal <= 0 ? [] : (looper(mkArr(grpSize, minVal), maxVal)[1])
return maxVal <= 0 ? [] : (looper(mkArr(grpSize, minVal), maxVal)[1])
.filter(sumsTo(trgVal))
.filter(noInnerZero)
.filter(noInnerZero);
.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 targetVal = cells - sumArr(blocks);
const numGaps = cells - sumArr(blocks);
const combine = zipArr([...blocks])
const fullLine = (numGaps === 0 && grpSize === 2);
const combine = zipArr([...blocks]);
permutations(grpSize, targetVal)
const choices = 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(output)
};
};



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>5 cells. Blocks: 2,1
<pre>
5 cells. Blocks: 2,1
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>