Add rule generator

This commit is contained in:
Lukas | AstroGD 2024-06-06 16:26:02 +02:00
parent b39a6a16c4
commit 0f2aaaa2a0
Signed by: AstroGD
GPG Key ID: 82A5E6C236C535AA
16 changed files with 194 additions and 19 deletions

View File

@ -3,16 +3,27 @@
"es2021": true, "es2021": true,
"node": true "node": true
}, },
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"], "extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"overrides": [], "overrides": [],
"parser": "@typescript-eslint/parser", "parser": "@typescript-eslint/parser",
"parserOptions": { "parserOptions": {
"ecmaVersion": "latest", "ecmaVersion": "latest",
"sourceType": "module" "sourceType": "module"
}, },
"plugins": ["@typescript-eslint"], "plugins": [
"@typescript-eslint"
],
"rules": { "rules": {
"indent": ["error", 4], "indent": [
"quotes": ["error", "double"] "error",
2
],
"quotes": [
"error",
"double"
]
} }
} }

3
.gitignore vendored
View File

@ -135,3 +135,6 @@ data
# AstrOGD Userdata directory # AstrOGD Userdata directory
userdata userdata
# output
output.cnf

View File

@ -1,6 +1,6 @@
{ {
"printWidth": 150, "printWidth": 150,
"tabWidth": 4, "tabWidth": 2,
"semi": true, "semi": true,
"singleQuote": false, "singleQuote": false,
"quoteProps": "consistent", "quoteProps": "consistent",

15
generateRules.ts Normal file
View File

@ -0,0 +1,15 @@
import generateRules, { getLength } from "./src/rules";
import fs from "fs";
import { v } from "./src/util";
const constValues: number[] = [];
const rules = generateRules(constValues);
const output = fs.createWriteStream("output.cnf");
output.write(`p cnf ${v(9, 9, 9)} ${getLength(constValues)}\n`);
for (const rule of rules) {
output.write(rule + "\n");
}
output.end();

View File

@ -1 +0,0 @@
console.log("Hello world!");

View File

@ -2,7 +2,7 @@
"name": "@tu-darmstadt/aupl-ss24-sudoku-solver", "name": "@tu-darmstadt/aupl-ss24-sudoku-solver",
"version": "1.0.0-dev.1", "version": "1.0.0-dev.1",
"description": "A tool converting a given sudoku into DIMACS CNF to be solved with a SAT solver, and reverting the solution from the SAT solver back into an easy readable sudoku visualitsation", "description": "A tool converting a given sudoku into DIMACS CNF to be solved with a SAT solver, and reverting the solution from the SAT solver back into an easy readable sudoku visualitsation",
"main": "dist/index.js", "main": "dist/generateRules.js",
"types": "dist/types/", "types": "dist/types/",
"directories": { "directories": {
"bin": "dist/bin/" "bin": "dist/bin/"

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;

View File

@ -1,7 +1,6 @@
{ {
"compilerOptions": { "compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */ /* Visit https://aka.ms/tsconfig to read more about this file */
/* Projects */ /* Projects */
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
@ -9,7 +8,6 @@
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
/* Language and Environment */ /* Language and Environment */
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, "target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
@ -23,7 +21,6 @@
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
/* Modules */ /* Modules */
"module": "commonjs" /* Specify what module code is generated. */, "module": "commonjs" /* Specify what module code is generated. */,
// "rootDir": "./", /* Specify the root folder within your source files. */ // "rootDir": "./", /* Specify the root folder within your source files. */
@ -37,12 +34,10 @@
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
"resolveJsonModule": true /* Enable importing .json files. */, "resolveJsonModule": true /* Enable importing .json files. */,
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */ // "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
/* JavaScript Support */ /* JavaScript Support */
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
/* Emit */ /* Emit */
"declaration": true /* Generate .d.ts files from TypeScript and JavaScript files in your project. */, "declaration": true /* Generate .d.ts files from TypeScript and JavaScript files in your project. */,
"declarationMap": true /* Create sourcemaps for d.ts files. */, "declarationMap": true /* Create sourcemaps for d.ts files. */,
@ -67,14 +62,12 @@
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
"declarationDir": "./dist/types" /* Specify the output directory for generated declaration files. */, "declarationDir": "./dist/types" /* Specify the output directory for generated declaration files. */,
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
/* Interop Constraints */ /* Interop Constraints */
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */, "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
/* Type Checking */ /* Type Checking */
"strict": true /* Enable all strict type-checking options. */, "strict": true /* Enable all strict type-checking options. */,
"noImplicitAny": true /* Enable error reporting for expressions and declarations with an implied 'any' type. */, "noImplicitAny": true /* Enable error reporting for expressions and declarations with an implied 'any' type. */,
@ -95,9 +88,12 @@
"noPropertyAccessFromIndexSignature": true /* Enforces using indexed accessors for keys declared using an indexed type. */, "noPropertyAccessFromIndexSignature": true /* Enforces using indexed accessors for keys declared using an indexed type. */,
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
/* Completeness */ /* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */ "skipLibCheck": true /* Skip type checking all .d.ts files. */
} },
"include": [
"generateRules.ts",
"visualizeResult.ts"
]
} }

0
visualizeResult.ts Normal file
View File