import Foundation enum Difficulty: String, CaseIterable, Codable, Identifiable, Sendable { case easy, medium, hard, expert, master var id: String { rawValue } var title: String { switch self { case .easy: "Easy" case .medium: "Medium" case .hard: "Hard" case .expert: "Expert" case .master: "Master" } } var subtitle: String { switch self { case .easy: "Warm up and find your rhythm" case .medium: "A little more thought, a little more fun" case .hard: "Settle in and let logic guide you" case .expert: "For those who love a good deduction" case .master: "Take on a puzzle with fewer clues" } } // Levels currently use clue counts, rather than a human solving-technique rating. var clues: Int { switch self { case .easy: 43 case .medium: 36 case .hard: 31 case .expert: 27 case .master: 24 } } var multiplier: Int { (Self.allCases.firstIndex(of: self) ?? 0) + 1 } } struct SudokuPuzzle: Sendable { let givens: [Int] let solution: [Int] } struct SeededGenerator: RandomNumberGenerator { private var state: UInt64 init(seed: UInt64) { state = seed } mutating func next() -> UInt64 { state &+= 0x9e3779b97f4a7c15 var value = state value = (value ^ (value >> 30)) &* 0xbf58476d1ce4e5b9 value = (value ^ (value >> 27)) &* 0x94d049bb133111eb return value ^ (value >> 31) } } enum SudokuEngine { static func generate(difficulty: Difficulty, seed: UInt64) -> SudokuPuzzle { var rng = SeededGenerator(seed: seed) let digits = Array(1...9).shuffled(using: &rng) let rows = (0..<3).shuffled(using: &rng).flatMap { band in (0..<3).shuffled(using: &rng).map { band * 3 + $0 } } let columns = (0..<3).shuffled(using: &rng).flatMap { stack in (0..<3).shuffled(using: &rng).map { stack * 3 + $0 } } let solution = rows.flatMap { row in columns.map { column in digits[(row * 3 + row / 3 + column) % 9] } } var best = solution // Retry removal orders; every accepted removal preserves a single solution. for _ in 0..<6 { var board = solution var remaining = 81 for index in (0..<81).shuffled(using: &rng) { guard remaining > difficulty.clues else { break } let old = board[index] board[index] = 0 if solutionCount(board, limit: 2) == 1 { remaining -= 1 } else { board[index] = old } } if board.filter({ $0 != 0 }).count < best.filter({ $0 != 0 }).count { best = board } if remaining == difficulty.clues { break } } return SudokuPuzzle(givens: best, solution: solution) } static func solutionCount(_ input: [Int], limit: Int = 2) -> Int { guard input.count == 81, input.allSatisfy({ (0...9).contains($0) }), limit > 0 else { return 0 } var board = input var rows = [Int](repeating: 0, count: 9) var columns = rows var boxes = rows for index in 0..<81 where board[index] != 0 { let r = index / 9, c = index % 9, b = r / 3 * 3 + c / 3 let bit = 1 << board[index] guard rows[r] & bit == 0, columns[c] & bit == 0, boxes[b] & bit == 0 else { return 0 } rows[r] |= bit; columns[c] |= bit; boxes[b] |= bit } var count = 0 func search() { guard count < limit else { return } var chosen = -1, mask = 0, minimum = 10 for index in 0..<81 where board[index] == 0 { let r = index / 9, c = index % 9, b = r / 3 * 3 + c / 3 let available = 0x3FE & ~(rows[r] | columns[c] | boxes[b]) let size = available.nonzeroBitCount if size == 0 { return } if size < minimum { chosen = index; mask = available; minimum = size } if size == 1 { break } } if chosen == -1 { count += 1; return } let r = chosen / 9, c = chosen % 9, b = r / 3 * 3 + c / 3 for digit in 1...9 where mask & (1 << digit) != 0 { let bit = 1 << digit board[chosen] = digit rows[r] |= bit; columns[c] |= bit; boxes[b] |= bit search() rows[r] &= ~bit; columns[c] &= ~bit; boxes[b] &= ~bit board[chosen] = 0 if count >= limit { return } } } search() return count } static func arePeers(_ first: Int, _ second: Int) -> Bool { first / 9 == second / 9 || first % 9 == second % 9 || (first / 27 == second / 27 && first % 9 / 3 == second % 9 / 3) } static func dateKey(_ date: Date, calendar: Calendar = .current) -> String { let parts = calendar.dateComponents([.year, .month, .day], from: date) return String(format: "%04d-%02d-%02d", parts.year ?? 2026, parts.month ?? 1, parts.day ?? 1) } static func dailySeed(_ key: String) -> UInt64 { key.utf8.reduce(14695981039346656037) { ($0 ^ UInt64($1)) &* 1099511628211 } } }