Add a bounded puzzle thinking-depth analysis tool

This commit is contained in:
Hadrian Burkhardt
2026-11-02 10:06:32 +01:00
parent 010b90bef5
commit c6168f46ac
8 changed files with 1617 additions and 2 deletions
+409
View File
@@ -0,0 +1,409 @@
[
{
"depth": 1,
"cellCount": 9,
"constraints": [
{
"name": "C0",
"cells": [
0,
1,
2,
3,
7
],
"total": 2
},
{
"name": "C1",
"cells": [
2,
6,
7,
8
],
"total": 2
},
{
"name": "C2",
"cells": [
1,
2,
3,
4,
5,
8
],
"total": 3
},
{
"name": "C3",
"cells": [
1,
3,
4,
5,
7
],
"total": 2
},
{
"name": "C4",
"cells": [
2,
5,
7
],
"total": 1
},
{
"name": "C5",
"cells": [
4,
6,
8
],
"total": 2
},
{
"name": "C6",
"cells": [
0,
1,
2,
3,
4,
5
],
"total": 3
},
{
"name": "C7",
"cells": [
1,
2,
7
],
"total": 1
},
{
"name": "C8",
"cells": [
1,
2,
4,
5,
6,
8
],
"total": 3
}
],
"initial": [
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1
],
"solution": [
0,
0,
1,
1,
1,
0,
1,
0,
0
],
"title": "One hypothetical assumption"
},
{
"cellCount": 16,
"initial": [
0,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1
],
"constraints": [
{
"name": "C0",
"cells": [
2,
4,
6,
9,
10,
13
],
"total": 3
},
{
"name": "C1",
"cells": [
1,
5,
6,
9,
12,
15
],
"total": 3
},
{
"name": "C2",
"cells": [
2,
4,
5,
9,
14,
15
],
"total": 3
},
{
"name": "C3",
"cells": [
0,
4,
5,
8,
11,
12
],
"total": 3
},
{
"name": "C4",
"cells": [
1,
3,
5,
12,
13,
14
],
"total": 3
},
{
"name": "C5",
"cells": [
0,
3,
6,
9,
11,
12
],
"total": 3
},
{
"name": "C6",
"cells": [
4,
6,
7,
8,
9,
15
],
"total": 3
},
{
"name": "C7",
"cells": [
0,
5,
6,
8,
10,
15
],
"total": 3
},
{
"name": "C8",
"cells": [
1,
3,
4,
8,
9,
13
],
"total": 3
},
{
"name": "C9",
"cells": [
5,
6,
7,
8,
11,
13
],
"total": 3
},
{
"name": "C10",
"cells": [
0,
4,
7,
8,
13,
14
],
"total": 3
},
{
"name": "C11",
"cells": [
1,
2,
6,
10,
14,
15
],
"total": 3
},
{
"name": "C12",
"cells": [
0,
2,
7,
12,
13,
15
],
"total": 3
},
{
"name": "C13",
"cells": [
3,
5,
6,
11,
14,
15
],
"total": 3
},
{
"name": "C14",
"cells": [
1,
5,
7,
10,
11,
12
],
"total": 3
},
{
"name": "C15",
"cells": [
3,
4,
7,
10,
12,
14
],
"total": 3
},
{
"name": "C16",
"cells": [
1,
2,
7,
9,
12,
14
],
"total": 3
},
{
"name": "C17",
"cells": [
2,
3,
5,
9,
10,
12
],
"total": 3
},
{
"name": "C18",
"cells": [
0,
1,
7,
8,
9,
11
],
"total": 3
},
{
"name": "C19",
"cells": [
2,
3,
4,
11,
12,
13
],
"total": 3
}
],
"solution": [
0,
0,
0,
0,
0,
0,
0,
0,
1,
1,
1,
1,
1,
1,
1,
1
],
"depth": 2,
"title": "Two nested hypothetical assumptions"
}
]
+271
View File
@@ -0,0 +1,271 @@
import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'package:flutter_test/flutter_test.dart';
import 'package:lumen/domain/book.dart';
import 'package:lumen/domain/puzzle.dart';
import 'package:lumen/domain/thinking.dart';
bool _satisfies(List<int> cells, List<Constraint> constraints) => constraints
.every((c) => c.cells.fold<int>(0, (sum, i) => sum + cells[i]) == c.total);
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
test(
'successive deductions are snapshot waves, independent of clue order',
() {
final constraints = [
Constraint([0], 1, 'A'),
Constraint([0, 1], 1, 'B'),
Constraint([1, 2], 1, 'C'),
Constraint([2, 3], 1, 'D'),
];
final initial = List.filled(4, -1);
for (final ordered in [constraints, constraints.reversed.toList()]) {
final report = ThinkingAnalyzer(4, ordered).analyze(initial);
expect(report.status, ThinkingStatus.solved);
expect(report.rounds.length, 4);
expect(report.rounds.map((r) => r.newCells), [1, 1, 1, 1]);
expect(report.maxAssumptionDepthUsed, 0);
expect(report.comparisonSteps, 0);
expect(report.cells, [1, 0, 1, 0]);
expect(initial, [-1, -1, -1, -1]);
}
},
);
test('overlap cancellation is a deduction, not hypothetical lookahead', () {
final constraints = [
Constraint([0, 1], 1, 'A'),
Constraint([0, 1, 2], 1, 'B'),
Constraint([2, 3], 1, 'C'),
Constraint([0, 3], 1, 'D'),
];
final report = ThinkingAnalyzer(4, constraints).analyze(List.filled(4, -1));
expect(report.status, ThinkingStatus.solved);
expect(report.initialDirectMoves, 0);
expect(report.comparisonSteps, greaterThan(0));
expect(report.maxAssumptionDepthUsed, 0);
expect(report.cells, [0, 1, 0, 1]);
});
final fixtures = jsonDecode(
File('test/fixtures/thinking_depth.json').readAsStringSync(),
) as List;
for (final fixture in fixtures) {
test('recognises ${fixture['depth']} levels of hypothetical reasoning', () {
final n = fixture['cellCount'] as int;
final initial = (fixture['initial'] as List).cast<int>();
final constraints = [
for (final c in fixture['constraints'])
Constraint((c['cells'] as List).cast<int>(), c['total'], c['name']),
];
final analyzer = ThinkingAnalyzer(n, constraints);
final depth = fixture['depth'] as int;
final limited = analyzer.analyze(initial, maxLookahead: depth - 1);
expect(limited.status, ThinkingStatus.depthLimit);
expect(limited.lookaheadDepth, isNull);
expect(limited.solutionCount, 1);
expect(limited.cells, contains(-1));
final solved = analyzer.analyze(initial, maxLookahead: depth);
expect(solved.status, ThinkingStatus.solved);
expect(solved.maxAssumptionDepthUsed, depth);
expect(solved.lookaheadDepth, depth);
expect(solved.cells, fixture['solution']);
expect(solved.assumptionSteps, greaterThan(0));
expect(
solved.steps
.where((s) => s.rule == ThinkingRule.assumption)
.every((s) => s.proof != null),
true,
);
// Independently enumerate completions to validate each rejection. The
// analyzer itself receives no answer and must not copy the fixture solution.
final known = List<int>.of(initial);
for (final round in solved.rounds) {
for (final step in round.steps) {
if (step.rule == ThinkingRule.assumption) {
final mark = step.marks.entries.single;
var alternatives = 0;
for (var mask = 0; mask < (1 << n); mask++) {
final cells = List.generate(n, (i) => mask >> i & 1);
if (cells[mark.key] != step.rejectedValue) continue;
if (List.generate(
n,
(i) => i,
).any((i) => known[i] >= 0 && known[i] != cells[i])) {
continue;
}
if (_satisfies(cells, constraints)) alternatives++;
}
expect(
alternatives,
0,
reason: 'Rejected branch must be impossible',
);
}
for (final mark in step.marks.entries) {
known[mark.key] = mark.value;
}
}
}
});
}
test(
'multiple answers are not reported as a need to think or guess deeper',
() {
final report = ThinkingAnalyzer(2, [
Constraint([0, 1], 1, 'A'),
]).analyze([-1, -1]);
expect(report.status, ThinkingStatus.nonUnique);
expect(report.solutionCount, 2);
expect(report.assumptionSteps, 0);
},
);
test('contradicting and impossible clues are reported explicitly', () {
for (final constraints in [
[
Constraint([0], 0, 'A'),
Constraint([0], 1, 'B'),
],
[
Constraint([0], 2, 'Impossible'),
],
]) {
final report = ThinkingAnalyzer(1, constraints).analyze([-1]);
expect(report.status, ThinkingStatus.inconsistent);
expect(report.solutionCount, 0);
expect(report.message, isNotEmpty);
}
});
test('a search-budget exit leaves depth and uniqueness unresolved', () {
final fixture = fixtures.last;
final constraints = [
for (final c in fixture['constraints'])
Constraint((c['cells'] as List).cast<int>(), c['total'], c['name']),
];
final report = ThinkingAnalyzer(
fixture['cellCount'],
constraints,
).analyze((fixture['initial'] as List).cast<int>(), nodeLimit: 1);
expect(report.status, ThinkingStatus.nodeLimit);
expect(report.searchNodes, 1);
expect(report.solutionCount, isNull);
expect(report.lookaheadDepth, isNull);
expect(report.cells, contains(-1));
expect(report.toJson()['status'], 'nodeLimit');
});
test('fully marked boards are validated, including incorrect marks', () {
final analyzer = ThinkingAnalyzer(2, [
Constraint([0, 1], 1, 'A'),
]);
final valid = analyzer.analyze([0, 1]);
expect(valid.status, ThinkingStatus.solved);
expect(valid.rounds, isEmpty);
expect(valid.resolvedCells, 0);
expect(analyzer.analyze([1, 1]).status, ThinkingStatus.inconsistent);
});
test('invalid input encodings and constraint indices fail clearly', () {
expect(
() => ThinkingAnalyzer(2, [
Constraint([0, 0], 1, 'Duplicate'),
]),
throwsArgumentError,
);
expect(
() => ThinkingAnalyzer(2, [
Constraint([2], 1, 'Outside'),
]),
throwsArgumentError,
);
final analyzer = ThinkingAnalyzer(2, []);
expect(() => analyzer.analyze([0]), throwsArgumentError);
expect(() => analyzer.analyze([2, -1]), throwsArgumentError);
expect(
() => analyzer.analyze([-1, -1], maxLookahead: 4),
throwsArgumentError,
);
expect(() => analyzer.analyze([-1, -1], nodeLimit: 0), throwsArgumentError);
});
test(
'classification and forced marks agree with exhaustive small systems',
() {
final rng = Random(38);
for (var trial = 0; trial < 200; trial++) {
const n = 6;
final constraints = List.generate(1 + rng.nextInt(8), (c) {
final cells = [
for (var i = 0; i < n; i++)
if (rng.nextBool()) i,
];
return Constraint(cells, rng.nextInt(cells.length + 1), 'C$c');
});
final initial = List.generate(
n,
(_) => rng.nextInt(5) == 0 ? rng.nextInt(2) : -1,
);
final completions = <List<int>>[];
for (var mask = 0; mask < 1 << n; mask++) {
final cells = List.generate(n, (i) => mask >> i & 1);
if (List.generate(
n,
(i) => i,
).any((i) => initial[i] >= 0 && initial[i] != cells[i])) {
continue;
}
if (_satisfies(cells, constraints)) completions.add(cells);
}
final report = ThinkingAnalyzer(n, constraints).analyze(initial);
expect(
report.solutionCount,
min(2, completions.length),
reason: 'Trial $trial',
);
if (completions.isEmpty) {
expect(report.status, ThinkingStatus.inconsistent);
}
if (completions.length > 1) {
expect(report.status, ThinkingStatus.nonUnique);
}
for (final step in report.steps) {
for (final mark in step.marks.entries) {
expect(
completions.every((s) => s[mark.key] == mark.value),
true,
reason: 'Unsound deduction in trial $trial',
);
}
}
}
},
);
test(
'all campaign traces solve the picture without hypothetical assumptions',
() async {
final book = await Book.load();
for (var index = 0; index < book.levels.length; index++) {
final p = book.levels[index].puzzle();
final report = ThinkingAnalyzer(49, p.constraints).analyze(p.initial);
expect(report.status, ThinkingStatus.solved, reason: 'Campaign $index');
expect(report.cells, p.solution);
expect(report.solutionCount, 1);
expect(report.maxAssumptionDepthUsed, 0);
expect(
report.rounds.fold<int>(report.givens, (sum, r) => sum + r.newCells),
49,
);
if (index < 2) expect(report.comparisonSteps, 0);
if (index >= 2) expect(report.comparisonSteps, greaterThan(0));
expect(report.toJson()['remainingCells'], isEmpty);
}
},
);
}