Public
Edited
Jan 12, 2023
1 fork
1 star
Insert cell
# Combinatorics
Insert cell
r = _.range(0, 12)
Insert cell
c = [...G.clone.combination(r, 9)]
Insert cell
G = require('generatorics');
Insert cell
_(c)
.filter((it) => it.includes(0) && _.sum(it) == 45)
//.map((it) => _.reduce(_.without(it, 0), _.multiply))
.value()
Insert cell
rownames = _.range(1, 10).map((r) => _.range(1, 10).map((c) => `R${r}C${c}`))
Insert cell
allDiffRows = (arr) =>
_.uniq(arr.map(_.sortBy).map((it) => it.join(","))).length == 9
Insert cell
allDiffVals = (arr) => _.every(arr, (it) => _.uniq(it).length == 9)
Insert cell
allSumTo45 = (arr) => _.every(arr, (it) => _.sum(it) == 45)
Insert cell
allHave0 = (arr) => _.every(arr, (it) => it.includes(0))
Insert cell
fulfillsExpectations = (arr) => [
allDiffRows(arr),
allDiffVals(arr),
allHave0(arr),
allSumTo45(arr)
]
Insert cell
rows = [
[7, 11, 2, 10, 6, 1, 5, 0, 3],
[6, 9, 0, 5, 4, 7, 11, 1, 2],
[5, 4, 1, 3, 0, 9, 6, 10, 7],
[3, 0, 4, 2, 1, 5, 9, 11, 10],
[8, 1, 11, 7, 9, 3, 2, 4, 0],
[2, 7, 9, 8, 10, 0, 1, 3, 5],
[4, 2, 6, 9, 8, 10, 0, 5, 1],
[0, 8, 5, 1, 2, 6, 3, 9, 11],
[10, 3, 7, 0, 5, 4, 8, 2, 6]
]
Insert cell
fulfillsExpectations(rows)
Insert cell
cols = (rows) => _.range(9).map((i) => rows.map((row) => row[i]))
Insert cell
fulfillsExpectations(cols(rows))
Insert cell
boxes = (rows) =>
_.range(0, 9, 3).flatMap((x) =>
_.range(0, 9, 3).map((y) =>
_.range(3).flatMap((dx) => _.range(3).map((dy) => rows[x + dx][y + dy]))
)
)
Insert cell
fulfillsExpectations(boxes(rows))
Insert cell
_.concat(rows, cols(rows), boxes(rows))
.map((r) => "[" + _.join(r, ",") + "]")
.join("\n")
Insert cell
crange = function* ([x1, y1], [x2, y2]) {
if (x1 != x2 && y1 != y2) throw `Not a line! ${[x1, y1, x2, y2]}`;
const xa = Math.min(x1, x2),
xz = Math.max(x1, x2),
ya = Math.min(y1, y2),
yz = Math.max(y1, y2);
for (let x = xa; x <= xz; ++x) {
for (let y = ya; y <= yz; ++y) {
yield `(${x},${y})`;
}
}
}
Insert cell
border = function* (corners) {
for (let i = 0; i < corners.length; ++i) {
const j = (i + 1) % corners.length;
yield* [...crange(corners[i], corners[j])];
}
}
Insert cell
_.uniq([
...border([
[1, 1],
[23, 1],
[23, 16],
[1, 16]
])
]).join(",")
Insert cell
_.uniq([
...border([
[1, 1],
[9, 1],
[9, 12],
[22, 12],
[22, 17],
[1,17]
])
]).join(",")
Insert cell
_.range(1, 9)
.map((a) =>
_.range(a + 1, 10)
.map((b) => `R${a}!=R${b}`)
.join(",")
)
.join(",\n")
Insert cell
function allDiff(sets) {
_.uniq(sets.map(_.sort).join()).length == sets.length;
}
Insert cell
Insert cell
checks = (solution) => ({
allDiff: _.every(solution, (it) => _.uniq(it).length == 9),
theSecret: _.every(solution, (it) => _.sum(it) == 45),
noClassic: _.every(solution, (it) => it.includes(0)),
setsDiff:
_.uniq(solution.map(_.sortBy).map((it) => it.join(","))).length == 9,
missing1: solution.some((it) => !it.includes(1))
})
Insert cell
solutions.map(checks)
Insert cell
_.uniq(
solutions.map((s) =>
s
.map(_.sortBy)
.map((it) => it.join())
.sort()
.join()
)
)
Insert cell
h`<style>
td{border:1px solid black;}

</style>
<table>${solutions[7].map(
(row) =>
h`<tr>${row.map(
(cell) =>
h`<td>${cell}</td>
`
)}</tr>`
)}</table>`
Insert cell
h=htl.html
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more