Add rule generator

This commit is contained in:
2024-06-06 16:26:02 +02:00
parent b39a6a16c4
commit 0f2aaaa2a0
16 changed files with 194 additions and 19 deletions

View File

@ -0,0 +1,29 @@
import { v } from "../util";
export default function* allValuesBlock(): Generator<number[], null, undefined> {
let count = 0;
for (const x of [1, 4, 7]) {
for (const y of [1, 4, 7]) {
for (let w = 1; w < 10; w++) {
yield [
v(x, y, w),
v(x, y + 1, w),
v(x, y + 2, w),
v(x + 1, y, w),
v(x + 1, y + 1, w),
v(x + 1, y + 2, w),
v(x + 2, y, w),
v(x + 2, y + 1, w),
v(x + 2, y + 2, w),
];
count++;
}
}
}
console.log("allValuesBlock: " + count);
return null;
}
export const length = 3 * 3 * 9;

View File

@ -0,0 +1,17 @@
import { v } from "../util";
export default function* allValuesColumn(): Generator<number[], null, undefined> {
let count = 0;
for (let x = 1; x < 10; x++) {
for (let w = 1; w < 10; w++) {
yield [v(x, 1, w), v(x, 2, w), v(x, 3, w), v(x, 4, w), v(x, 5, w), v(x, 6, w), v(x, 7, w), v(x, 8, w), v(x, 9, w)];
count++;
}
}
console.log("allValuesColumn: " + count);
return null;
}
export const length = 9 * 9;

17
src/rules/allValuesRow.ts Normal file
View File

@ -0,0 +1,17 @@
import { v } from "../util";
export default function* allValuesRow(): Generator<number[], null, undefined> {
let count = 0;
for (let y = 1; y < 10; y++) {
for (let w = 1; w < 10; w++) {
yield [v(1, y, w), v(2, y, w), v(3, y, w), v(4, y, w), v(5, y, w), v(6, y, w), v(7, y, w), v(8, y, w), v(9, y, w)];
count++;
}
}
console.log("allValuesRow: " + count);
return null;
}
export const length = 9 * 9;

View File

@ -0,0 +1,17 @@
import { v } from "../util";
export default function* atLeastOneValue(): Generator<number[], null, undefined> {
let count = 0;
for (let x = 1; x < 10; x++) {
for (let y = 1; y < 10; y++) {
yield [v(x, y, 1), v(x, y, 2), v(x, y, 3), v(x, y, 4), v(x, y, 5), v(x, y, 6), v(x, y, 7), v(x, y, 8), v(x, y, 9)];
count++;
}
}
console.log("atLeastOneValue: " + count);
return null;
}
export const length = 9 * 9;

15
src/rules/fixedValues.ts Normal file
View File

@ -0,0 +1,15 @@
export default function* fixedValues(values: number[]): Generator<number[], null, undefined> {
let count = 0;
for (const value of values) {
yield [value];
count++;
}
console.log("fixedValues: " + count);
return null;
}
export function getLength(values: number[]): number {
return values.length;
}

24
src/rules/index.ts Normal file
View File

@ -0,0 +1,24 @@
import allValuesBlock, { length as l1 } from "./allValuesBlock";
import allValuesColumn, { length as l2 } from "./allValuesColumn";
import allValuesRow, { length as l3 } from "./allValuesRow";
import atLeastOneValue, { length as l4 } from "./atLeastOneValue";
import fixedValuesHandler, { getLength as getL5 } from "./fixedValues";
import maxOneValue, { length as l6 } from "./maxOneValue";
export default function* generateRules(fixedValues: number[]): Generator<string, null, undefined> {
const generators = [allValuesBlock(), allValuesColumn(), allValuesRow(), atLeastOneValue(), maxOneValue(), fixedValuesHandler(fixedValues)];
for (const generator of generators) {
let rule = generator.next().value;
while (rule !== null) {
yield rule.join(" ") + " 0";
rule = generator.next().value;
}
}
return null;
}
export function getLength(fixedValues: number[]): number {
return l1 + l2 + l3 + l4 + getL5(fixedValues) + l6;
}

21
src/rules/maxOneValue.ts Normal file
View File

@ -0,0 +1,21 @@
import { v } from "../util";
export default function* maxOneValue(): Generator<number[], null, undefined> {
let count = 0;
for (let x = 1; x < 10; x++) {
for (let y = 1; y < 10; y++) {
for (let w = 1; w < 9; w++) {
for (let w2 = w + 1; w2 < 10; w2++) {
yield [-v(x, y, w), -v(x, y, w2)];
count++;
}
}
}
}
console.log("maxOneValue: " + count);
return null;
}
export const length = 9 * 9 * (8 + 7 + 6 + 5 + 4 + 3 + 2 + 1);

11
src/util.ts Normal file
View File

@ -0,0 +1,11 @@
import { assert } from "console";
export function v(x: number, y: number, w: number): number {
assert(1 <= x && x <= 9);
assert(1 <= y && y <= 9);
assert(1 <= w && w <= 9);
return x + 9 * (y - 1) + 81 * (w - 1);
}
export type N = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;