Make special mechanics essential in postcard and story puzzles
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:lumen/domain/puzzle.dart';
|
||||
import 'package:lumen/domain/thinking.dart';
|
||||
|
||||
void main() {
|
||||
for (final path in [
|
||||
'assets/campaign.json',
|
||||
'assets/stories/midnight-invitation/campaign.json',
|
||||
]) {
|
||||
final data =
|
||||
jsonDecode(File(path).readAsStringSync()) as Map<String, dynamic>;
|
||||
final levels = data['levels'] as List;
|
||||
for (final index in [4, 6, 8, 10]) {
|
||||
final level = levels[index] as Map<String, dynamic>;
|
||||
final solution = (level['picture'] as String)
|
||||
.split('')
|
||||
.map(int.parse)
|
||||
.toList();
|
||||
final rng = Random(8721 + index);
|
||||
final trails = <List<int>>[];
|
||||
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 (solution[cells[1]] == 1 &&
|
||||
solution[cells[0]] + solution[cells[2]] == 1) {
|
||||
trails.add(cells);
|
||||
}
|
||||
}
|
||||
}
|
||||
Map<String, dynamic>? chosen;
|
||||
List<int>? givens;
|
||||
for (var attempt = 0; attempt < 600; attempt++) {
|
||||
final p = Puzzle(7, solution);
|
||||
for (final area
|
||||
in (level['areas'] as String)
|
||||
.split(';')
|
||||
.where((a) => a.isNotEmpty)) {
|
||||
final v = area.split(',').map(int.parse).toList();
|
||||
p.regions.add(
|
||||
Region(
|
||||
String.fromCharCode(65 + p.regions.length),
|
||||
v[0],
|
||||
v[1],
|
||||
v[2],
|
||||
v[3],
|
||||
),
|
||||
);
|
||||
}
|
||||
p.omittedRow = rng.nextInt(7);
|
||||
p.omittedColumn = rng.nextInt(7);
|
||||
final mechanics = <String, dynamic>{
|
||||
'omittedRow': p.omittedRow,
|
||||
'omittedColumn': p.omittedColumn,
|
||||
};
|
||||
final marked = <int>{};
|
||||
if (index == 6 || index == 10) {
|
||||
final trail = trails[rng.nextInt(trails.length)];
|
||||
p.footprintTrails.add(trail);
|
||||
marked.addAll(trail);
|
||||
mechanics['trail'] = trail;
|
||||
} else {
|
||||
final a = rng.nextInt(49);
|
||||
final options = [
|
||||
for (var b = 0; b < 49; b++)
|
||||
if (solution[a] != solution[b] &&
|
||||
a ~/ 7 != b ~/ 7 &&
|
||||
a % 7 != b % 7)
|
||||
b,
|
||||
];
|
||||
if (options.isEmpty) continue;
|
||||
final b = options[rng.nextInt(options.length)];
|
||||
p.lanternPairs.add(LanternPair(a, b));
|
||||
marked.addAll([a, b]);
|
||||
mechanics['lantern'] = [a, b];
|
||||
}
|
||||
for (var j = 0; j < 49; j++) {
|
||||
p.given[j] = !marked.contains(j);
|
||||
}
|
||||
final full = p.constraints;
|
||||
final order = [
|
||||
for (var j = 0; j < 49; j++)
|
||||
if (p.given[j]) j,
|
||||
]..shuffle(rng);
|
||||
for (final cell in order) {
|
||||
p.given[cell] = false;
|
||||
if (!LogicSolver(49, full).analyze(p.initial).solved ||
|
||||
p.countSolutions() != 1) {
|
||||
p.given[cell] = true;
|
||||
}
|
||||
}
|
||||
final without = full
|
||||
.where(
|
||||
(c) =>
|
||||
c.name != 'Lantern pair' &&
|
||||
!c.name.startsWith('Footprint trail'),
|
||||
)
|
||||
.toList();
|
||||
final logic = LogicSolver(49, full).analyze(p.initial);
|
||||
final thinking = ThinkingAnalyzer(
|
||||
49,
|
||||
full,
|
||||
).analyze(p.initial, maxLookahead: 0);
|
||||
final start = LogicSolver(49, full).analyze(p.initial, useAreas: false);
|
||||
final paced = path == 'assets/campaign.json'
|
||||
? logic.areaDeductions <= (index < 8 ? 5 : 6) &&
|
||||
start.cells.where((v) => v >= 0).length -
|
||||
p.given.where((v) => v).length >=
|
||||
7
|
||||
: thinking.rounds.length >= 4 &&
|
||||
thinking.rounds.length <= 8 &&
|
||||
thinking.comparisonSteps >= 2;
|
||||
if (paced &&
|
||||
p.countSolutions() == 1 &&
|
||||
LogicSolver(49, full).analyze(p.initial).solved &&
|
||||
!LogicSolver(49, full).analyze(p.initial, useAreas: false).solved &&
|
||||
LogicSolver(49, without).countSolutions(p.initial) > 1) {
|
||||
chosen = mechanics;
|
||||
givens = [
|
||||
for (var j = 0; j < 49; j++)
|
||||
if (p.given[j]) j,
|
||||
];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (chosen == null) throw StateError('No authored board: $path $index');
|
||||
level['mechanics'] = chosen;
|
||||
level['clues'] = givens!.join(',');
|
||||
stderr.writeln('$path ${level['id']}: ${givens.length} givens, $chosen');
|
||||
}
|
||||
File(path).writeAsStringSync(
|
||||
'${const JsonEncoder.withIndent(' ').convert(data)}\n',
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
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');
|
||||
}
|
||||
Reference in New Issue
Block a user