<commit_before>import Foundation

struct Queen {
    let x: Int
    let y: Int

    func canAttack(_ other: Queen) -> Bool {
        if other.x == x || other.y == y {
            return true
        }
        if abs(other.x - x) == abs(other.y - y) {
            return true
        }
        return false
    }
}

extension Collection where Iterator.Element == Queen {

    func fits(x: Int, y: Int) -> Bool {
        let queen = Queen(x: x, y: y)
        return filter { $0.canAttack(queen) }
            .isEmpty
    }

}

func board(_ current: [Queen] = [], y: Int = 0) -> [Queen]? {
    if current.count == 8 {
        return current
    }
    for x in 0..<8 where current.fits(x: x, y: y) {
        var copy = current
        copy.append(Queen(x: x, y: y))
        if let newBoard = board(copy, y: y + 1) {
            return newBoard
        }
    }
    return nil
}

// Task: Find a configuration of 8 Queens in a chess board
// where no queen can Attack another one

let result = board()?.map { (x: $0.x, y: $0.y) } ?? []

print(result)<commit_msg>Counting all the solutions to the queens problem<commit_after>import Foundation

protocol ChessPiece {
    var x: Int { get }
    var y: Int { get }
    init(x: Int, y: Int)
    func canAttack(x: Int, y: Int) -> Bool
}

extension ChessPiece {
    
    func canAttack(_ other: ChessPiece) -> Bool {
        return canAttack(x: other.x, y: other.y)
    }
    
}

struct Queen: ChessPiece {
    let x: Int
    let y: Int
    
    func canAttack(x: Int, y: Int) -> Bool {
        if self.x == x || self.y == y {
            return true
        }
        if abs(self.x - x) == abs(self.y - y) {
            return true
        }
        return false
    }
}

extension Collection where Iterator.Element == ChessPiece {
    
    func fits(x: Int, y: Int) -> Bool {
        return filter { $0.canAttack(x: x, y: y) }
                .isEmpty
    }
    
}

typealias Board = [ChessPiece]

func boards(_ current: Board = [], y: Int = 0) -> [Board] {
    if current.count == 8 {
        return [current]
    }
    return (0..<8)
            .filter { current.fits(x: $0, y: y) }
            .reduce([]) { results, x in
                var current = current
                current.append(Queen(x: x, y: y))
                var results = results
                results.append(contentsOf: boards(current, y: y + 1))
                return results
            }
    
}

// Task: Find all configurations of 8 Queens in a chess board
// where no queen can Attack another one

let result = boards()

print("\(result.count) possibilities:")
print(result)
{'fraction_non_alphanumeric': 0.125626204238921, 'fraction_numerical': 0.006551059730250481, 'mean_word_length': 1.7412882787750792, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6476975', 'n_tokens_mistral': 872, 'n_tokens_neox': 836, 'n_words': 275}