SudokuEngine.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import Foundation
  2. enum Difficulty: String, CaseIterable, Codable, Identifiable, Sendable {
  3. case easy, medium, hard, expert, master
  4. var id: String { rawValue }
  5. var title: String {
  6. switch self {
  7. case .easy: "Easy"
  8. case .medium: "Medium"
  9. case .hard: "Hard"
  10. case .expert: "Expert"
  11. case .master: "Master"
  12. }
  13. }
  14. var subtitle: String {
  15. switch self {
  16. case .easy: "Warm up and find your rhythm"
  17. case .medium: "A little more thought, a little more fun"
  18. case .hard: "Settle in and let logic guide you"
  19. case .expert: "For those who love a good deduction"
  20. case .master: "Take on a puzzle with fewer clues"
  21. }
  22. }
  23. // Levels currently use clue counts, rather than a human solving-technique rating.
  24. var clues: Int {
  25. switch self {
  26. case .easy: 43
  27. case .medium: 36
  28. case .hard: 31
  29. case .expert: 27
  30. case .master: 24
  31. }
  32. }
  33. var multiplier: Int { (Self.allCases.firstIndex(of: self) ?? 0) + 1 }
  34. }
  35. struct SudokuPuzzle: Sendable {
  36. let givens: [Int]
  37. let solution: [Int]
  38. }
  39. struct SeededGenerator: RandomNumberGenerator {
  40. private var state: UInt64
  41. init(seed: UInt64) { state = seed }
  42. mutating func next() -> UInt64 {
  43. state &+= 0x9e3779b97f4a7c15
  44. var value = state
  45. value = (value ^ (value >> 30)) &* 0xbf58476d1ce4e5b9
  46. value = (value ^ (value >> 27)) &* 0x94d049bb133111eb
  47. return value ^ (value >> 31)
  48. }
  49. }
  50. enum SudokuEngine {
  51. static func generate(difficulty: Difficulty, seed: UInt64) -> SudokuPuzzle {
  52. var rng = SeededGenerator(seed: seed)
  53. let digits = Array(1...9).shuffled(using: &rng)
  54. let rows = (0..<3).shuffled(using: &rng).flatMap { band in
  55. (0..<3).shuffled(using: &rng).map { band * 3 + $0 }
  56. }
  57. let columns = (0..<3).shuffled(using: &rng).flatMap { stack in
  58. (0..<3).shuffled(using: &rng).map { stack * 3 + $0 }
  59. }
  60. let solution = rows.flatMap { row in
  61. columns.map { column in digits[(row * 3 + row / 3 + column) % 9] }
  62. }
  63. var best = solution
  64. // Retry removal orders; every accepted removal preserves a single solution.
  65. for _ in 0..<6 {
  66. var board = solution
  67. var remaining = 81
  68. for index in (0..<81).shuffled(using: &rng) {
  69. guard remaining > difficulty.clues else { break }
  70. let old = board[index]
  71. board[index] = 0
  72. if solutionCount(board, limit: 2) == 1 {
  73. remaining -= 1
  74. } else {
  75. board[index] = old
  76. }
  77. }
  78. if board.filter({ $0 != 0 }).count < best.filter({ $0 != 0 }).count { best = board }
  79. if remaining == difficulty.clues { break }
  80. }
  81. return SudokuPuzzle(givens: best, solution: solution)
  82. }
  83. static func solutionCount(_ input: [Int], limit: Int = 2) -> Int {
  84. guard input.count == 81, input.allSatisfy({ (0...9).contains($0) }), limit > 0 else { return 0 }
  85. var board = input
  86. var rows = [Int](repeating: 0, count: 9)
  87. var columns = rows
  88. var boxes = rows
  89. for index in 0..<81 where board[index] != 0 {
  90. let r = index / 9, c = index % 9, b = r / 3 * 3 + c / 3
  91. let bit = 1 << board[index]
  92. guard rows[r] & bit == 0, columns[c] & bit == 0, boxes[b] & bit == 0 else { return 0 }
  93. rows[r] |= bit; columns[c] |= bit; boxes[b] |= bit
  94. }
  95. var count = 0
  96. func search() {
  97. guard count < limit else { return }
  98. var chosen = -1, mask = 0, minimum = 10
  99. for index in 0..<81 where board[index] == 0 {
  100. let r = index / 9, c = index % 9, b = r / 3 * 3 + c / 3
  101. let available = 0x3FE & ~(rows[r] | columns[c] | boxes[b])
  102. let size = available.nonzeroBitCount
  103. if size == 0 { return }
  104. if size < minimum { chosen = index; mask = available; minimum = size }
  105. if size == 1 { break }
  106. }
  107. if chosen == -1 { count += 1; return }
  108. let r = chosen / 9, c = chosen % 9, b = r / 3 * 3 + c / 3
  109. for digit in 1...9 where mask & (1 << digit) != 0 {
  110. let bit = 1 << digit
  111. board[chosen] = digit
  112. rows[r] |= bit; columns[c] |= bit; boxes[b] |= bit
  113. search()
  114. rows[r] &= ~bit; columns[c] &= ~bit; boxes[b] &= ~bit
  115. board[chosen] = 0
  116. if count >= limit { return }
  117. }
  118. }
  119. search()
  120. return count
  121. }
  122. static func arePeers(_ first: Int, _ second: Int) -> Bool {
  123. first / 9 == second / 9 || first % 9 == second % 9 ||
  124. (first / 27 == second / 27 && first % 9 / 3 == second % 9 / 3)
  125. }
  126. static func dateKey(_ date: Date, calendar: Calendar = .current) -> String {
  127. let parts = calendar.dateComponents([.year, .month, .day], from: date)
  128. return String(format: "%04d-%02d-%02d", parts.year ?? 2026, parts.month ?? 1, parts.day ?? 1)
  129. }
  130. static func dailySeed(_ key: String) -> UInt64 {
  131. key.utf8.reduce(14695981039346656037) { ($0 ^ UInt64($1)) &* 1099511628211 }
  132. }
  133. }