Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Board {
- let grid : [[Int]] = [
- [5, 3, 4, 6, 7, 8, 9, 1, 2],
- [6, 7, 2, 1, 9, 5, 3, 4, 8],
- [1, 9, 8, 3, 4, 2, 5, 6, 7],
- [8, 5, 9, 7, 6, 1, 4, 2, 3],
- [4, 2, 6, 8, 5, 3, 7, 9, 1],
- [7, 1, 3, 9, 2, 4, 8, 5, 6],
- [9, 6, 1, 5, 3, 7, 2, 8, 4],
- [2, 8, 7, 4, 1, 9, 6, 3, 5],
- [3, 4, 5, 2, 8, 6, 1, 7, 9],
- ]
- // (0,0) (0,1) (0,2)
- // (1,0) (1,1) (1,2)
- // (2,0) (2,1) (2,2)
- func validateArray(_ array: [Int]) -> Bool {
- var set = Set(array)
- return set.count == 9
- }
- func validateGridAt(_ row: Int, _ col: Int) -> Bool {
- for i in 0...2 {
- for j in 0...2 {
- set.insert(grid[i + row][j + col])
- }
- }
- if set.count != 9 {
- return false
- }
- }
- func validate() -> Bool {
- for i in 0..<9 {
- // if !validateArray(grid[i]) {
- // return false
- // }
- var set = Set<Int>()
- for j in 0..<9 {
- set.insert(grid[i][j])
- }
- if set.count != 9 {
- return false
- }
- }
- for j in 0..<9 {
- var set = Set<Int>()
- for i in 0..<9 {
- set.insert(grid[i][j])
- }
- if set.count != 9 {
- return false
- }
- }
- // 0,0,3,3,6,6
- // for i in stride(from: 0, to: 9, by: 3) {
- for k in [0, 3, 6] {
- for l in 0..<2 {
- if !validateGridAt(k, l) {
- return false
- }
- var set = Set<Int>()
- for i in 0...2 {
- for j in 0...2 {
- set.insert(grid[i + (l * 3)][j + (k * 3)])
- }
- }
- if set.count != 9 {
- return false
- }
- }
- for k in 0..<2 {
- var set = Set<Int>()
- for i in 0...2 {
- for j in 0...2 {
- set.insert(grid[i + (k * 3)][j])
- }
- }
- if set.count != 9 {
- return false
- }
- }
- return true
- }
- }
- // for all i for 0 a[0..j-1]
- // add them all to a set the count should be 9
- // i (0..2) , j (0,2)
- let b = Board()
- print(b.validate())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement