// Reproducible constraint authoring for the fixed Halloween picture silhouettes. // This never changes the authored pictures or story text. import 'dart:convert'; import 'dart:io'; import 'dart:math'; import 'package:lumen/domain/puzzle.dart'; import 'package:lumen/domain/thinking.dart'; void main() { final file = File('assets/stories/midnight-invitation/campaign.json'); final data = jsonDecode(file.readAsStringSync()) as Map; final reports = >[]; for (var index = 0; index < (data['levels'] as List).length; index++) { final record = data['levels'][index] as Map; final picture = (record['picture'] as String) .split('') .map(int.parse) .toList(); final random = Random(0x10312026 + index); Puzzle? best; ThinkingReport? bestReport; var bestScore = double.infinity; final trials = index < 2 ? 180 : 1200; for (var trial = 0; trial < trials; trial++) { final p = Puzzle(7, picture); final regionCount = index < 2 ? 0 : index < 4 ? 2 : 3; while (p.regions.length < regionCount) { final height = 3 + random.nextInt(2), width = 3 + random.nextInt(2); final r = Region( String.fromCharCode(65 + p.regions.length), random.nextInt(8 - height), random.nextInt(8 - width), height, width, ); if (p.regions.any( (old) => old.top == r.top && old.left == r.left && old.height == r.height && old.width == r.width, )) { continue; } if (p.regions.isNotEmpty) { final old = p.regions.last; final common = List.generate( 49, (i) => i, ).where((i) => old.contains(i, 7) && r.contains(i, 7)).length; if (common < 2 || common >= min(old.height * old.width, r.height * r.width)) { continue; } } p.regions.add(r); } final order = List.generate(49, (i) => i)..shuffle(random); for (final cell in order.take(2 + random.nextInt(3))) { p.given[cell] = true; } final solver = LogicSolver(49, p.constraints); var analysis = solver.analyze(p.initial); while (!analysis.solved && p.given.where((v) => v).length < 8) { final candidates = List.generate( 49, (i) => i, ).where((i) => analysis.cells[i] < 0).toList()..shuffle(random); var selected = candidates.first, mostKnown = -1; for (final cell in candidates.take(8)) { p.given[cell] = true; final a = solver.analyze(p.initial); final known = a.cells.where((v) => v >= 0).length; if (known > mostKnown) { selected = cell; mostKnown = known; } p.given[cell] = false; } p.given[selected] = true; analysis = solver.analyze(p.initial); } if (!analysis.solved) continue; for (final cell in order.where((i) => p.given[i])) { if (p.given.where((v) => v).length <= (index < 2 ? 2 : 0)) break; p.given[cell] = false; if (!solver.analyze(p.initial).solved) p.given[cell] = true; } final singles = solver.analyze(p.initial, useAreas: false); if (index >= 2 && (singles.solved || analysis.areaDeductions == 0)) { continue; } final givens = p.given.where((v) => v).length; final foothold = singles.cells.where((v) => v >= 0).length - givens; if (foothold < 4) continue; final report = ThinkingAnalyzer( 49, p.constraints, ).analyze(p.initial, maxLookahead: 0); if (report.status != ThinkingStatus.solved || report.assumptionSteps != 0) { continue; } final targetRounds = index < 2 ? 4.0 : 5.0 + index / 4; final targetComparison = index < 2 ? 0 : 2 + index ~/ 3; final score = (report.rounds.length - targetRounds).abs() * 3 + (report.comparisonSteps - targetComparison).abs() * 1.5 + givens * 1.2 + (index >= 4 ? (foothold - 12).abs() * .15 : 0); if (score < bestScore) { bestScore = score; best = p; bestReport = report; } if (score < 3.5) break; } if (best == null || bestReport == null) { throw StateError('No suitable constraints for ${record['id']}'); } if (best.countSolutions() != 1) { throw StateError('Non-unique ${record['id']}'); } record['clues'] = List.generate( 49, (i) => i, ).where((i) => best!.given[i]).join(','); record['areas'] = best.regions .map((r) => '${r.top},${r.left},${r.height},${r.width}') .join(';'); reports.add({ 'id': record['id'], 'givens': record['clues'], 'areas': record['areas'], 'report': bestReport.toJson(), }); stdout.writeln( '${index + 1}. ${record['id']}: ${bestReport.givens} givens, ${bestReport.rounds.length} rounds, ${bestReport.comparisonSteps} comparisons, no assumptions', ); } file.writeAsStringSync( '${const JsonEncoder.withIndent(' ').convert(data)}\n', ); File('assets/stories/midnight-invitation/puzzle-report.json') .writeAsStringSync( '${const JsonEncoder.withIndent(' ').convert(reports)}\n', ); }