153 lines
5.2 KiB
Dart
153 lines
5.2 KiB
Dart
import 'dart:io';
|
|
import 'dart:math';
|
|
|
|
import 'package:lumen/domain/postcards.dart';
|
|
import 'package:lumen/domain/puzzle.dart';
|
|
|
|
void main() {
|
|
final out = <String>[
|
|
'// Generated by tool/tune_postcard_puzzles.dart. Null keeps the original givens.',
|
|
'const postcardTunedGivens = <List<int>?>[',
|
|
];
|
|
final trailsOut = <String>[];
|
|
final pairsOut = <String>[];
|
|
for (var i = 0; i < islandPostcards.length; i++) {
|
|
for (var slot = 0; slot < 3; slot++) {
|
|
final p = islandPostcards[i].puzzle(slot, useTunedGivens: false);
|
|
bool special(Constraint c) =>
|
|
(p.lanternPairs.isNotEmpty && c.name == 'Lantern pair') ||
|
|
(p.mirrorPairs.isNotEmpty && c.equal) ||
|
|
(p.footprintTrails.isNotEmpty &&
|
|
c.name.startsWith('Footprint trail')) ||
|
|
(p.hiddenRow != null && c.name == 'Combined hidden total');
|
|
final full = p.constraints,
|
|
without = full.where((c) => !special(c)).toList();
|
|
if (full.length == without.length) {
|
|
out.add(' null,');
|
|
trailsOut.add(' null,');
|
|
pairsOut.add(' null,');
|
|
continue;
|
|
}
|
|
final marked = <int>{
|
|
for (final pair in [...p.lanternPairs, ...p.mirrorPairs]) ...[
|
|
pair.first,
|
|
pair.second,
|
|
],
|
|
for (final trail in p.footprintTrails) ...trail,
|
|
...p.hiddenTotalCells,
|
|
};
|
|
final initial = [
|
|
for (var j = 0; j < 49; j++)
|
|
if (!marked.contains(j)) j,
|
|
];
|
|
List<int>? best;
|
|
List<int>? bestTrail;
|
|
List<int>? bestPair;
|
|
final pairCandidates = <LanternPair>[];
|
|
if (p.lanternPairs.isNotEmpty) {
|
|
for (var a = 0; a < 49; a++) {
|
|
for (var b = a + 1; b < 49; b++) {
|
|
if (p.solution[a] != p.solution[b] &&
|
|
a ~/ 7 != b ~/ 7 &&
|
|
a % 7 != b % 7) {
|
|
pairCandidates.add(LanternPair(a, b));
|
|
}
|
|
}
|
|
}
|
|
pairCandidates.shuffle(Random(i * 79 + slot));
|
|
}
|
|
final trailCandidates = <List<int>>[];
|
|
if (p.footprintTrails.isNotEmpty) {
|
|
for (var a = 0; a < 49; a++) {
|
|
for (final step in [1, 7]) {
|
|
final cells = [a, a + step, a + 2 * step];
|
|
if (cells.last >= 49 || (step == 1 && a ~/ 7 != cells.last ~/ 7)) {
|
|
continue;
|
|
}
|
|
if (p.solution[cells[1]] == 1 &&
|
|
p.solution[cells[0]] + p.solution[cells[2]] == 1) {
|
|
trailCandidates.add(cells);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for (var attempt = 0; attempt < 200; attempt++) {
|
|
if (pairCandidates.isNotEmpty) {
|
|
p.lanternPairs
|
|
..clear()
|
|
..add(pairCandidates[attempt % pairCandidates.length]);
|
|
full
|
|
..clear()
|
|
..addAll(p.constraints);
|
|
without
|
|
..clear()
|
|
..addAll(full.where((c) => !special(c)));
|
|
}
|
|
if (trailCandidates.isNotEmpty) {
|
|
p.footprintTrails
|
|
..clear()
|
|
..add(trailCandidates[attempt % trailCandidates.length]);
|
|
full.clear();
|
|
full.addAll(p.constraints);
|
|
without
|
|
..clear()
|
|
..addAll(full.where((c) => !special(c)));
|
|
}
|
|
final order = List<int>.of(initial)
|
|
..shuffle(Random(39139 + i * 423 + slot * 11 + attempt));
|
|
final given = List.generate(
|
|
49,
|
|
(j) =>
|
|
initial.contains(j) &&
|
|
!p.footprintTrails.any((t) => t.contains(j)) &&
|
|
!p.lanternPairs.any((pair) => pair.contains(j)),
|
|
);
|
|
for (final j in order) {
|
|
given[j] = false;
|
|
final state = List.generate(49, (k) => given[k] ? p.solution[k] : -1);
|
|
if (!LogicSolver(49, full).analyze(state).solved ||
|
|
LogicSolver(49, full).countSolutions(state) != 1) {
|
|
given[j] = true;
|
|
}
|
|
}
|
|
final state = List.generate(49, (k) => given[k] ? p.solution[k] : -1);
|
|
if (LogicSolver(49, without).countSolutions(state) > 1) {
|
|
final found = [
|
|
for (var j = 0; j < 49; j++)
|
|
if (given[j]) j,
|
|
];
|
|
if (best == null || found.length < best.length) {
|
|
best = found;
|
|
if (p.footprintTrails.isNotEmpty) {
|
|
bestTrail = List.of(p.footprintTrails.single);
|
|
}
|
|
if (p.lanternPairs.isNotEmpty) {
|
|
bestPair = [
|
|
p.lanternPairs.single.first,
|
|
p.lanternPairs.single.second,
|
|
];
|
|
}
|
|
}
|
|
if (attempt >= 10) break;
|
|
}
|
|
}
|
|
stderr.writeln(
|
|
'${i + 1}/$slot ${best == null ? 'NO SOLUTION' : '${initial.length} -> ${best.length}'}',
|
|
);
|
|
if (best == null) throw StateError('Could not author $i/$slot');
|
|
out.add(' $best,');
|
|
trailsOut.add(bestTrail == null ? ' null,' : ' $bestTrail,');
|
|
pairsOut.add(bestPair == null ? ' null,' : ' $bestPair,');
|
|
}
|
|
}
|
|
out.add('];');
|
|
out.add(
|
|
'const postcardTunedTrails = <List<int>?>[\n${trailsOut.join('\n')}\n];',
|
|
);
|
|
out.add(
|
|
'const postcardTunedLanterns = <List<int>?>[\n${pairsOut.join('\n')}\n];',
|
|
);
|
|
File('lib/domain/postcard_tuned_givens.dart')
|
|
.writeAsStringSync('${out.join('\n')}\n');
|
|
}
|